Modifying PDF file using Python - GeeksforGeeks

02 Jul, 2021
The following article depicts how a PDF can be modified using python’s pylovepdf module. The Portable Document Format(PDF) is a file format developed by Adobe in 1993 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.
pylovepdf module can be downloaded using pip command:
pip install pylovepdf
The iLovePDF API i.e ‘pylovepdf’ module is organized around REST. Their API are predictable, resource-oriented URLs, and uses HTTP response codes to indicate API errors. They use built-in HTTP features, like HTTP authentication and HTTP verbs, which are understood by off-the-shelf HTTP clients. They support cross-origin resource sharing, allowing you to interact securely with their API from a client-side web application. With this API we can compress the PDF files, also can add watermark, convert them to images and even split them and vice -versa and lots of other stuff.
In order to do so first we need a public-key to use this module, for that login on to https://developer.ilovepdf.com/ and after login the public key will be visible in the ‘My Projects’ section. Below is the screenshot of the public key
Now since we have our public key, we can use this API for modifying any PDF file using the steps given below:
1. Creating a ILovePdf object using the public key
2. Uploading the PDF file
3. Processing the PDF file
4. Downloading the PDF file
Implementation of this module is depicted properly using examples. Click here for the PDF used in the examples provided in this article: 
Example 1: Compressing the PDF file
Python3
# importing the ilovepdf apifrom pylovepdf.ilovepdf import ILovePdf # public keypublic_key = 'paste_your_public_key_here' # creating a ILovePdf objectilovepdf = ILovePdf(public_key, verify_ssl=True) # assigning a new compress tasktask = ilovepdf.new_task('compress') # adding the pdf file to the tasktask.add_file('my_pdf.pdf') # setting the output folder directory# if no folder exist it will create onetask.set_output_folder('output_folder') # execute the tasktask.execute() # download the tasktask.download() # delete the tasktask.delete_current_task()
Before processing:
Output :
 After Processing: 
Example 2: Splitting the PDF
Python
# public keyfrom pylovepdf.ilovepdf import ILovePdfpublic_key = 'paste your code here' # importing the ilovepdf api # creating a ILovePdf objectilovepdf = ILovePdf(public_key, verify_ssl=True) # assigning a new split task tasktask = ilovepdf.new_task('split') # adding the pdf file to the tasktask.add_file('my_pdf.pdf')  # setting the output folder directory# if no folder exist it will create onetask.set_output_folder('output_folder') # execute the tasktask.execute() # download the tasktask.download() # delete the tasktask.delete_current_task()
Output :
After processing:
zip
 
sweetyty
python-utility
Technical Scripter 2020
Python
Technical Scripter
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