Take and convert Screenshot to PDF using Python - GeeksforGeeks

11 Jan, 2022
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. 
For installation of the library use the following commands:
PyAutoGUI:
pip install PyAutoGUI
PIL(Python imaging library):
pip install Pillow
img2pdf
pip install img2pdf
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 pyautoguifrom PIL import Image # Taking ScreenshottakeScreenshot = pyautogui.screenshot() # The path of Screenshot and r' is used for specifying raw stringscreenshotPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\screenshot.png' # Saving the screenshot in the given PathtakeScreenshot.save(screenshotPath) # Opening imageopen_image = Image.open(screenshotPath)convert_image = open_image.convert('RGB') # Output Pdf PathoutputpdfPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\output.pdf' # Saving the pdfopen_image.save(outputpdfPath)
Output:
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 pyautoguiimport img2pdf # Taking ScreenshottakeScreenshot = pyautogui.screenshot() # The path of Screenshot and r' is used for specifying raw stringscreenshotPath = r'C:\Users\Pranjal\Desktop\gfgarticle\PDF\screenshot.png' # Saving the screenshot in the given PathtakeScreenshot.save(screenshotPath) # Opening Img file objImgFile = open(screenshotPath, "rb") # Opening the Pdf file objPdfFile = open("output.pdf", "wb") # Converting Image File to PDFPdfFile.write(img2pdf.convert(ImgFile)) # Closing Image File ObjectImgFile.close() # Closing PDF File ObjectPdfFile.close()
Output:
anikakapoor
sweetyty
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list