The Wayback Machine - https://web.archive.org/web/20240624183613/https://www.geeksforgeeks.org/take-and-convert-screenshot-to-pdf-using-python/
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:



Similar Reads

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") Argument : filename or the full path you wish to save y
1 min read
Send PDF File through Email using pdf-mail module
pdf_mail module is that library of Python which helps you to send pdf documents through your Gmail account. Installing Library This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. pip install pdf-mail Function of pdf_mail This module only comes with a singl
2 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 Document Format) is one of the basic requirements in
3 min read
Convert Excel to PDF Using Python
Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. In this article, we will learn how to convert an Excel File to PDF File Using Python Here we will
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.0: This module converts a PDF to a PIL object. T
2 min read
Convert Docx to Pdf using docx2pdf Module in Python
Tired of having to use online docx to PDF converters with crappy interfaces and conversion limits? Then, look no further than your friendly neighborhood language python's docx2pdf module. This module is a hidden gem among the many modules for the python language. This module can be used to convert files singly or in bulk using the command line or a
2 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Converting CSV file to HTML using Pandas Framework. P
3 min read
How to convert a PDF file to TIFF file using Python?
This article will discover how to transform a PDF (Portable Document Format) file on your local drive into a TIFF (Tag Image File Format) file at the specified location. We'll employ Python's Aspose-Words package for this task. The aspose-words library will be used to convert a PDF file to a TIFF file. Aspose-Words: Aspose-Words for Python is a pot
3 min read
How to Convert a PDF to Document using Python?
To convert PDF files to Doc format you can use a Python module and it will make it straightforward for you in the conversion of pdf to doc. In this article, We'll explore converting a PDF document to a Doc file using Python. In this, we use the pdf2docx module as it contains built-in functionalities that will simplify the conversion process and won
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg