Skip to main content

Use Python to automate your daily tasks

In the days of ChatGPT, automating your daily tasks has never been so easy. You can supercharge your automation workflows if you gain some basic understanding of programming. Python is a beginner-friendly programming language and can be used in multiple scenarios. Here are some use cases to give you a taste of what you can do:

  • Merge PDFs using Python
  • Convert image format using Python
  • Enhance your images
  • Check website status
  • Backup important files

Once you understand the basics, you can then expand this knowledge to build your own custom scripts. Even if you ask ChatGPT to write a script for you, some understanding of basic concepts will be useful, so that you can be more specific in your instructions and potentially catch errors.

Note Install Python version 3and Visual Studio Code. If you're on a Mac or a newer version of Windows, Pythons should already be pre-installed.

Merge PDFs Using Python

Working with PDF files can be a pain...but it doesn't have to be. You don't even need any expensive software to handle PDFs: just a few lines of Python code will suffice.

In this tutorial, I'll show you how to easily merge multiple files without having to tinker in the Adobe Acrobat or a similar program, simply taking advantage of the capabilities of the PyPDF2 library. This method is much quicker and cheaper and all the tools used here are free.

  1. Organize your files
    Gather all PDFs you want to merge in the same folder.

  2. Install the PyPDF2 library

    Python doesn't include built-in tools to work with PDFs, so we'll need to import an external set of tools. A set of these is called a library.

    In the terminal, type pip install PyPDF2.

  3. Import PyPDF2 library

To be able to work with the tools included in the library we've just imported, we need to add them to our Python file. Go to Visual Studio code, and create a new file "app.py". Then add this code to the file:

import PyPDF2
from PyPDF import PdfMerger
  1. Use PdfMerger

We are now ready to work with the tools included in the PyPDF library. One of those is called PdfMerger, and as you might have guessed, allows us to merge PDFs. We need a container to hold our files in, as you would in a folder. In programming, this is called an object.

Create an object called "merger".
By adding the equal sign, I tell Python that "merger" is a single object that is part of the PdfMerger class. A class is a blueprint used to create an object based on it.

merger = PdfMerger()
  1. Combine the files

As in real life, we can use objects in different ways. Python allows us certain actions - these are called methods. One of those methods is "append".

This is called a method, and there are specific methods we can use built into the language.

We'll use append method to attach files in the the consecutive order.
As we want all files appended to the merger object

for pdf in ["file1.pdf", "file2.pdf", "file3.pdf", "file4.pdf"]:
merger.append(pdf)
  1. Now our files are ordered, but we want to create a new file containing all of them. We use a write() method to create a new file:
merger.write("my-new-merged-file.pdf")
  1. To stop the program from running indefinitely, you need to close it:
merger.close()

The resulting Python code:

import PyPDF2
from PyPDF import PdfMerger
merger = PdfMerger()
for pdf in ["file1.pdf", "file2.pdf", "file3.pdf", "file4.pdf"]:
merger.append(pdf)
merger.write("my-new-merged-file.pdf")
merger.close()

Convert image format using Python

There's plenty of image editing software out there, most notably Photoshop - but did you know you can also edit images using Python? While Pillow, the Python library used in this tutorial, won't replace Photoshop, it does help you to perform certain actions faster - and completely free.

In this introductory tutorial I'll show you how to convert image format using Pillow library. You can then build on that knowledge to progress to the more advanced concepts, such as Image filters.

I'm using Python version 3.

Tip! Some basic Python knowldege required!

Install Pillow

First, we need to prepare our paints and brushes, in our case it means installing the Pillow library.

In your terminal type:

pip install Pillow

Convert image format

Converting the image format in the Pillow library is quick and easy. In this example I'll show you how to convert a JPEG image to a PNG.

You can also use Pillow to convert to and from other popular image formats such as:

  • BMP
  • GIF
  • EPS
  • TIFF

Check out the docs for the full list of supported formats.

  1. In your Python file, import Image from the Pillow library. The Image module contains a class of the same name.
from PIL import Image
  1. Create an Image object and call the open method on it. Make sure that you add the correct path for the image location - my image is in the same folder as my Python file, so I don't need to add it.
img = Image.open('some-pic.jpg')
  1. Save as a png image: specify the file name and the extention (format) to save the image in the same folder. You can also save the image in a different location by adding the file path.
im.save('converted-pic.png')

The resulting Python code:

import Image

img = Image.open('some-pic.jpg')
im.save('converted-pic.png')