Convert PDF to Text in Python

Convert PDF to text in Python

Python is a feature-rich programming language. We can perform different operations on files in python using the different modules and libraries. In this article, we will discuss different ways to convert a pdf file to text in python.

Convert pdf to text using pypdf2

To convert a pdf to text in python, we can use the PyPDF2 module. You can install this module using PIP by executing the following command in the command prompt.

pip3 install PyPDF2

To convert the pdf file to text, we will first open the file using the open() function in the “rb” mode. i.e.Instead of the file contents, we will read the file in binary mode. The open() function takes the filename as the first input argument and the mode as the second input argument. After opening the file, it returns a file object that we assign to the variable myFile.  

After getting the file object, we will create a pdfFileReader object using the PdfFileReader() function defined in the PyPDF2 module. The PdfFileReader() function accepts the file object containing the pdf file as the input argument and returns a pdfFileReader object. Using the pdfFileReader, we can convert the PDF file to text. 

Also, we will open a file “output.txt” in write mode to save the text data extracted from the pdf file using the open() function. We will assign this file object to a variable output_file.

To create the text file from the PDF file, we will first find the number of pages in the PDF file. For this, we will use the numPages attribute of the pdfFileReader object.

Output:

You can view the pdf file used in this example here.

After getting the number of pages in the PDF file, we will use a for loop to process all the pages of the pdf file. In the for loop, we will extract each page from the PDF file using the getPage() method. The getPage() method, when invoked on a pdfFileReader object, accepts the page number as an input argument and returns a pageObject containing data from the specified page of the PDF file.

After getting the pageObject, we will use the extractText() method to extract text from the current page. After that, we will write the extracted text to the output text file.

After extracting the text from all the pages in pdf, we will close both the text file and the pdf file. Otherwise, the changes will not be saved.

Output:

Convert pdf to text using PDFminer

Instead of using PyPDF2, we can use the PDFminer.six module to convert a pdf file to a text file. You can install the PDFminer.six module as follows.

pip3 install pdfminer.six

The PDFminer.six module provides us with the extract_text() function that we can use to convert the PDF file to a text file. The extract_text() function accepts a file object representing the PDF file as an input argument and returns the text data in the file. 

After opening the PDF file and the output text file, we can extract the text from the PDF file using the extract_text() function. Then, we will save the text data to the output file. Don’t forget to close the files before the termination of the program.

Output:

Conclusion

In this article, we have discussed two ways to convert pdf to a text file in python. Out of all these, the approach using the PyPDF2 module is the fastest in terms of execution speed. However, you can use any approach at your convenience.

I hope you enjoyed reading this article. Stay tuned for more informative articles. 

Happy Learning! 

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.