avatarSonali Pandey

Summary

This article provides a comprehensive guide on how to manipulate PDF files using Python, with a focus on using the PyPDF2 library within an Anaconda environment.

Abstract

The article titled "Working with PDF files in Python" is a tutorial aimed at data scientists and natural language processing enthusiasts who frequently encounter PDFs in their work. It covers the installation of the Anaconda environment and the PyPDF2 library, which is essential for PDF operations in Python. The author guides readers through opening a PDF, extracting and reading its contents, adding a page to another PDF, and rotating PDF pages. The tutorial emphasizes the importance of handling PDFs efficiently due to their widespread use across various domains. Practical code examples are provided for each operation, and the article concludes with an invitation for readers to explore further possibilities with PDF manipulation in Python.

Opinions

  • The author suggests that dealing with text data in PDF format can be cumbersome for natural language processing tasks, implying a preference for simpler text formats.
  • There is an emphasis on the versatility of the PyPDF2 library, highlighting its capabilities beyond mere text extraction, such as merging, rotating, and appending PDF pages.
  • The author expresses a personal inclination towards symmetry, indicating a subjective preference for properly oriented PDF pages, which can be achieved through the techniques described in the article.
  • The article is structured to be beginner-friendly, with step-by-step instructions and encouragement for readers to follow along using their preferred Integrated Development Environment (IDE).
  • There is an underlying assumption that readers are interested in both natural language processing and Python programming, as the tutorial is tailored to this intersection of interests.

Working with PDF files in Python

Photo by Chris Ried on Unsplash

Introduction

PDF stands for Portable Document Format. It is needless to say that all of us are very much familiar with it as well. PDF documents are used widely in almost every domain ranging from businesses to tech. However, have you wondered how do the natural language enthusiasts and coders deal with it? A large number of data scientists receive reports from different departments in the form of PDFs for analysis, processing, and much more.

Natural Language Processing deals with a large amount of text processing. It is easy to deal with the text data if it is present in the simple text format but it becomes cumbersome when the same data is presented in a PDF format. In this article, we will learn to work with PDF files in Python and understand how can we perform operations such as open, read, write, merge, and much more on them. Excited to dive deeper into it? Let’s get started!!

This article is structured as follows:

  1. Installing Anaconda environment and the PyPDF2 library
  2. Opening a PDF in Python
  3. Extracting the text from a PDF file in Python
  4. Reading the contents of a PDF in Python
  5. Adding a page to another PDF using Python
  6. Rotating the pages of a PDF in python

Anaconda environment and the PyPDF2 library

Anaconda environment provides support for one of the versions of the programming language R or Python and the associated packages. The Anaconda distribution can be downloaded from here for Windows. If you are a Mac or Linux user, just google about the instructions and you will be good to go.

PyPDF2 is a Python library dedicated to performing operations on PDF files. It allows the merging of PDF files, rotation of a page in the PDF file, extraction of text from a PDF file, and much more. To install PyPDF2 library, go to Anaconda Command prompt-> Desired Location->type the command: pip install pypdf2. The following image shows how to do it:

Since I have already installed it, the image above shows that the requirement is already satisfied. Once we are done with this, we can move ahead to explore our first basic operation of how to open a PDF file in Python.

Please note that I will be using Jupyter Notebook to execute the codes. Feel free to use any IDE/environment of your choice.

Open a PDF file in Python

Opening a PDf file in Python is no herculean task. It is as simple as reading a text file in Python. However, you should be careful about the mode you are opening the PDF file in. This is because a PDF file is different from a text file and closer to an image file in its nature. Also, PyPDF2 is used to extract text from the PDF’s that are made using the word processor. However, this does not imply all the word processors and it often requires software to extract text from a PDF file. To open a pdf file in Python, follow the code given below:

Here I have opened the file in “wb” mode which means that if the file does not exist, it will be created automatically. Also, the file is being considered as a binary file.

Extracting the contents from a PDF file in Python

Once the file is opened, it is now time to extract the contents of the file. Just follow the code as shown in the image below and you will be good to go!

Reading the contents from a PDF file in Python

Extracted the contents successfully? Let’s read the contents then! Refer to the image below to see what the pdf contains so that you can compare it with the code output.

Now, follow the code below to read the contents of the PDF file:

Also, make sure you close the file after you are done with it to avoid any discrepancies that might arise in the future.

Adding a page to another PDF using Python

Appending text to a PDF file is very different from a text file. You cannot do it directly like how you do it for a text file. So, in this operation, we will be copying a page of one PDF file to another. Do not worry, it’s not as complicated as it sounds. You can follow the demo given below and add a page to your PDF file:

Hurrah! Now you can see the samples_output.pdf file created. The image below shows the contents of samples_output file:

Rotating the pages of a PDF in python

Being a person obsessed with symmetry, I hate to see the pages which are not in the proper position in my PDF. If you are facing a similar situation, trust me this piece of code will be a boon for you.

Voila! You have successfully learned the basic operations that can be performed on a PDF file in Python. I hope this was an interesting journey for all the Natural Language and Python enthusiasts. Hope to see you soon!! Thanks for reading :)

NLP
Text Processing
Pdf
Python
Files In Python
Recommended from ReadMedium