Screenshot to PDF using Python (example included)

In this tutorial, you’ll see the complete steps to take a screenshot and then convert it to PDF using Python.

To accomplish this goal you’ll need to use the following packages:

  • pyautogui
  • pillow (pil)

Steps to take a screenshot and then convert it to PDF using Python

Step 1: Install the relevant packages

If you haven’t already done so, install the pyautogui package using the command below:

pip install pyautogui

Note that depending on your version of pip, you may run into an issue with the installation of pyautogui.

Next, install the Pillow (PIL) package using this command:

pip install Pillow

Step 2: Take the screenshot and then convert it to PDF

You can use the following template to assist you in taking the screenshot and converting it to PDF using Python:

import pyautogui
from PIL import Image

my_screenshot = pyautogui.screenshot()
screenshot_path = r'path to save screenshot\file name.png'
my_screenshot.save(screenshot_path)

image_1 = Image.open(screenshot_path)
im_1 = image_1.convert('RGB')
pdf_path = r'path where the pdf will be stored\new file name.pdf'
im_1.save(pdf_path)

In the above code, you’ll need to make sure to specify:

  • The ‘path to save screenshot\file name.png
  • The ‘path where the pdf will be stored\new file name.pdf

For illustration purposes, let’s specify the following paths (note that you’ll need to modify the paths to reflect the location where the files will be stored on your computer):

  • The ‘path to save screenshot\file name.png‘ = ‘C:\Users\Ron\Desktop\Test\screenshot_1.png’
  • The ‘path where the pdf will be stored\new file name.pdf‘ = ‘C:\Users\Ron\Desktop\Test\pdf_1.pdf’

So for this example, the complete Python code to take the screenshot and then convert it to PDF is as follows:

import pyautogui
from PIL import Image

my_screenshot = pyautogui.screenshot()
screenshot_path = r'C:\Users\Ron\Desktop\Test\screenshot_1.png'
my_screenshot.save(screenshot_path)

image_1 = Image.open(screenshot_path)
im_1 = image_1.convert('RGB')
pdf_path = r'C:\Users\Ron\Desktop\Test\pdf_1.pdf'
im_1.save(pdf_path)

Don’t forget to place “r” before the paths to avoid any errors when running the code. Once you run the above code (adjusted to your paths), you’ll see the screenshot file (png), as well as the PDF file.

Optionally, you could enhance the code to keep only the PDF file after taking the screenshot. In that case, the screenshot will be deleted once the PDF file is saved at your specified location.

Here is the modified template that you may use:

import pyautogui
from PIL import Image
import os

my_screenshot = pyautogui.screenshot()
screenshot_path = r'path to save screenshot\file name.png'
my_screenshot.save(screenshot_path)

image_1 = Image.open(screenshot_path)
im_1 = image_1.convert('RGB')
pdf_path = r'path where the pdf will be stored\new file name.pdf'
im_1.save(pdf_path)

os.remove(screenshot_path)

Note that for the above code, you’ll need to use the os package in order to remove the screenshot (png file) once the PDF file is created.