In this short guide, you’ll see the full 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
Next, install the Pillow (PIL) package using this command:
pip install Pillow
Step 2: Take a screenshot and then convert it to PDF
You can use the following template to take a screenshot and convert it to a PDF file 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)
Here is an example (you’ll need to modify the paths based on your needs):
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)
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 can keep only the PDF file after taking the screenshot (the screenshot file will be deleted):
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)