Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Setting file offsets in Python
Next article icon

Modifying PDF file using Python

Last Updated : 18 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 api
from pylovepdf.ilovepdf import ILovePdf
 
# public key
public_key = 'paste_your_public_key_here'
 
# creating a ILovePdf object
ilovepdf = ILovePdf(public_key, verify_ssl=True)
 
# assigning a new compress task
task = ilovepdf.new_task('compress')
 
# adding the pdf file to the task
task.add_file('my_pdf.pdf')
 
# setting the output folder directory
# if no folder exist it will create one
task.set_output_folder('output_folder')
 
# execute the task
task.execute()
 
# download the task
task.download()
 
# delete the task
task.delete_current_task()
 
 

Before processing:

Output :

 After Processing: 

Example 2: Splitting the PDF

Python




# public key
from pylovepdf.ilovepdf import ILovePdf
public_key = 'paste your code here'
 
# importing the ilovepdf api
 
# creating a ILovePdf object
ilovepdf = ILovePdf(public_key, verify_ssl=True)
 
# assigning a new split task
task = ilovepdf.new_task('split')
 
# adding the pdf file to the task
task.add_file('my_pdf.pdf')
 
 
# setting the output folder directory
# if no folder exist it will create one
task.set_output_folder('output_folder')
 
# execute the task
task.execute()
 
# download the task
task.download()
 
# delete the task
task.delete_current_task()
 
 

Output :

After processing:

zip

 



Next Article
Setting file offsets in Python

R

rakshitarora
Improve
Article Tags :
  • Python
  • Technical Scripter
  • python-utility
  • Technical Scripter 2020
Practice Tags :
  • python

Similar Reads

  • Handling TOML files using Python
    In this article, we will see how we can manipulate TOML files using tomli/tomlib module in Python. What is a TOML file? TOML stands for Tom's Obvious, Minimal Language, here Tom refers to the creator of this language Tom-Preston Werner. It is a file format, especially for files that hold some kind o
    5 min read
  • Setting file offsets in Python
    Prerequisite: seek(), tell() Python makes it extremely easy to create/edit text files with a minimal amount of code required. To access a text file we have to create a filehandle that will make an offset at the beginning of the text file. Simply said, offset is the position of the read/write pointer
    4 min read
  • Read JSON file using Python
    The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
    4 min read
  • Modify XML files with Python
    Python|Modifying/Parsing XML Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.The design goals of XML focus on simplicity, generality, and usability across the Internet.It is a textua
    4 min read
  • Python - Get file id of windows file
    File ID is a unique file identifier used on windows to identify a unique file on a Volume. File Id works similar in spirit to a inode number found in *nix Distributions. Such that a fileId could be used to uniquely identify a file in a volume. We would be using an command found in Windows Command Pr
    3 min read
  • Python code formatting using Black
    Writing well-formatted code is very important, small programs are easy to understand but as programs get complex they get harder and harder to understand. At some point, you can’t even understand the code written by you. To avoid this, it is needed to write code in a readable format. Here Black come
    3 min read
  • Writing to file in Python
    Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail. Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following t
    4 min read
  • File Mode in Python
    In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho
    5 min read
  • File Handling in Python
    File handling refers to the process of performing operations on a file such as creating, opening, reading, writing and closing it, through a programming interface. It involves managing the data flow between the program and the file system on the storage device, ensuring that data is handled safely a
    7 min read
  • Read File As String in Python
    Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences