Take and convert Screenshot to PDF using Python
Last Updated :
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.
Installation
For installation of the library use the following commands:
pip install PyAutoGUI
- PIL(Python imaging library):
pip install Pillow
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:
Similar Reads
How to take screenshots using python? Python is a widely used general-purpose language. It allows performing a variety of tasks. One of them can be taking a screenshot. It provides a module named pyautogui which can be used to take the screenshot. pyautogui takes pictures as a PIL(python image library) which supports opening, manipulati
1 min read
Convert Text and Text File to PDF using Python PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Do
3 min read
How to take screenshot using Selenium in Python ? Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file. Syntax : driver.save_screenshot("image.png") Argum
1 min read
How to Take Screenshot of a Div Using JavaScript? A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website. The below steps show the method to take a screenshot of a <div> element using JavaScript. ApproachIn this approach, we will create a blank HTML docume
2 min read
Convert PDF to CSV using Python Python is a high-level, general-purpose, and very popular programming language. Python programming language (the latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. Python Programming Language is very well sui
2 min read
Convert PDF to Image using Python Many tools are available on the internet for converting a PDF to an image. In this article, we are going to write code for converting pdf to image and make a handy application in python. Before writing the code we need to install the required module pdf2image and poppler.Modules Neededpdf2image 1.14
2 min read