Open In App

Take and convert Screenshot to PDF using Python

Last Updated : 11 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In order to take and convert a screenshot to PDF, firstly the PyAutoGUI can be used which is an automation library in python which can control mouse, keyboard and can handle many GUI control tasks. Secondly, for the conversion PIL(Python Imaging Library) of python can be used which provides image processing facility and it supports many file formats and their conversion. The second library which can be used for conversion is img2pdf which provides as the name suggests the lossless and faster conversion of image to pdf. 

Installation

For installation of the library use the following commands:

  • PyAutoGUI:
pip install PyAutoGUI
  • PIL(Python imaging library):
pip install Pillow
  • img2pdf
pip install img2pdf

Approach #1

In this approach, we are using the PIL library of python. First, the screenshot is taken using the screenshot() function of PyAutoGUI library of python. After that, the output of the screenshot is saved. The open() method of PIL library is used to open the image and then convert() method to convert the image to RGB which is then saved with .pdf extension in the given path. Alternatively, you can also provide the screenshot/image by providing the path inside the open() method.

Note: The r’ is used so that the string is treated as a raw string.

Implementation:

Python3




import pyautogui
from PIL import Image
 
# Taking Screenshot
takeScreenshot = pyautogui.screenshot()
 
# The path of Screenshot and r' is used for specifying raw string
screenshotPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\screenshot.png'
 
# Saving the screenshot in the given Path
takeScreenshot.save(screenshotPath)
 
# Opening image
open_image = Image.open(screenshotPath)
convert_image = open_image.convert('RGB')
 
# Output Pdf Path
outputpdfPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\output.pdf'
 
# Saving the pdf
open_image.save(outputpdfPath)


Output:

Approach #2

In this approach, the img2pdf library is used for the conversion. Firstly, the screenshot is taken using the screenshot() method of PyAutoGUI library of python. After the screenshot is opened using the open() method and “rb” as a parameter is passed for opening the file in the binary format. After that, the output file which is pdf is opened using open() method bt passing the “wb” parameter (used for writing in binary). The write() function is called and convert() method of img2pdf is passed with screenshot object. At last, both objects are closed so that they flush any unwritten information.

The main advantage to the method is that it is fast as compared to PIL and it is also lossless conversion with small size. Alternatively, you can also provide the screenshot/image by providing the path inside the open() method.

Note: The screenshot/image does not contain an alpha channel since there is no method available that converts the RGBA to RGB in img2pdf.

Implementation:

Python3




import pyautogui
import img2pdf
 
# Taking Screenshot
takeScreenshot = pyautogui.screenshot()
 
# The path of Screenshot and r' is used for specifying raw string
screenshotPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\screenshot.png'
 
# Saving the screenshot in the given Path
takeScreenshot.save(screenshotPath)
 
# Opening Img file obj
ImgFile = open(screenshotPath, "rb")
 
# Opening the Pdf file obj
PdfFile = open("output.pdf", "wb")
 
# Converting Image File to PDF
PdfFile.write(img2pdf.convert(ImgFile))
 
# Closing Image File Object
ImgFile.close()
 
# Closing PDF File Object
PdfFile.close()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads