SoFunction
Updated on 2025-04-15

How to batch extract pdf text content in Python

The main steps in Python to batch extract PDF text content are: using a suitable PDF processing library, traversing PDF files, extracting text content, saving and extracting results. First of all, we need to choose a powerful and easy-to-use PDF processing library, such as PyMuPDF (fitz), PDFMiner, PyPDF2, etc. Next, iterate through the PDF files in the specified directory, use the selected PDF library to extract the text content of each PDF file, and save the extracted results to the specified format file, such as TXT or CSV files. The following will introduce these steps in detail and provide specific code examples.

1. Choose the right PDF processing library

In Python, there are a variety of libraries to choose from for processing PDF files. Commonly used ones include PyMuPDF (fitz), PDFMiner, PyPDF2, etc. Here is a brief introduction to these libraries:

  • PyMuPDF(fitz): Powerful function, supports text extraction, image extraction, page operation, etc.
  • PDFMiner: Focus on text extraction and supports a variety of text formats and layouts.
  • PyPDF2: Lightweight, mainly used for simple PDF operations, such as merging, splitting, etc.

This article mainly uses PyMuPDF (fitz) to extract PDF text content. PyMuPDF (fitz) is not only powerful, but also relatively simple to use.

2. Install the required libraries

Before we start writing code, we need to install the required Python library. PyMuPDF (fitz) can be installed using the following command:

pip install PyMuPDF

3. Traverse PDF files

We first need to iterate through all PDF files in the specified directory. This can be achieved using the os library. The following is a code example that traverses all PDF files in the specified directory:

import os

def get_pdf_files(directory):
pdf_files = []
for root, dirs, files in (directory):
for file in files:
if ('.pdf'):
pdf_files.append((root, file))
return pdf_files
directory = 'path/to/pdf/directory'
pdf_files = get_pdf_files(directory)
print(pdf_files)

4. Extract text content

Next, we use the PyMuPDF (fitz) library to extract the text content of each PDF file. Here is a code example for extracting PDF text content:

import fitz  # PyMuPDF

def extract_text_from_pdf(pdf_path):
text = ""
document = (pdf_path)
for page_num in range(len(document)):
page = document.load_page(page_num)
text += page.get_text()
return text

pdf_path = 'path/to/pdf/'
text = extract_text_from_pdf(pdf_path)
print(text)

5. Save the extract results

Finally, we save the extracted text content to the specified file. You can choose to save as a TXT or CSV file. Here is a code example that saves the extracted result:

def save_text_to_file(text, output_path):
with open(output_path, 'w', encoding='utf-8') as file:
(text)
output_path = 'path/to/output/'
save_text_to_file(text, output_path)

6. Complete sample code

Based on the above steps, we can write a complete script to batch extract the text content of all PDF files in the specified directory and save it to the TXT file:

import os
import fitz # PyMuPDF
def get_pdf_files(directory):
pdf_files = []
for root, dirs, files in (directory):
for file in files:
if ('.pdf'):
pdf_files.append((root, file))
return pdf_files
def extract_text_from_pdf(pdf_path):
text = ""
document = (pdf_path)
for page_num in range(len(document)):
page = document.load_page(page_num)
text += page.get_text()
return text
def save_text_to_file(text, output_path):
with open(output_path, 'w', encoding='utf-8') as file:
(text)
def batch_extract_text_from_pdfs(directory, output_directory):
pdf_files = get_pdf_files(directory)
for pdf_file in pdf_files:
text = extract_text_from_pdf(pdf_file)
output_path = (output_directory, (pdf_file).replace('.pdf', '.txt'))
save_text_to_file(text, output_path)
print(f"Extracted text from {pdf_file} to {output_path}")
input_directory = 'path/to/pdf/directory'
output_directory = 'path/to/output/directory'
batch_extract_text_from_pdfs(input_directory, output_directory)

7. Handle special circumstances

In actual applications, we may encounter some special situations, such as encrypted PDF files, PDF files that cannot extract text, etc. We can add corresponding processing logic to the code.

1. Process encrypted PDF files

For encrypted PDF files, we can try to open the file with a password. If there is no password, skip the file. Here is a code example for handling encrypted PDF files:

def extract_text_from_pdf(pdf_path, password=None):
text = ""
document = (pdf_path)
if document.is_encrypted:
if password:
(password)
else:
print(f"Skipping encrypted file: {pdf_path}")
return text
for page_num in range(len(document)):
page = document.load_page(page_num)
text += page.get_text()
return text
pdf_path = 'path/to/encrypted/pdf/'
password = 'your_password'
text = extract_text_from_pdf(pdf_path, password)
print(text)

2. Process PDF files that cannot extract text

Some PDF files may not be able to extract text content. We can add exception handling logic to the code and skip files that cannot extract text. Here is a code example for handling the inability to extract text PDF files:

def extract_text_from_pdf(pdf_path):
text = ""
try:
document = (pdf_path)
for page_num in range(len(document)):
page = document.load_page(page_num)
text += page.get_text()
except Exception as e:
print(f"Error extracting text from {pdf_path}: {e}")
return text
pdf_path = 'path/to/problematic/pdf/'
text = extract_text_from_pdf(pdf_path)
print(text)

8. Summary

This article details the steps of how to use Python to extract PDF text content, including selecting a suitable PDF processing library, traversing PDF files, extracting text content, saving extraction results, and handling special situations. Through these steps, we can efficiently extract the text content in PDF files in batches to meet the needs of practical applications.

In practical applications, we can further optimize and expand the code according to specific needs, such as adding multi-threaded or multi-process processing to improve efficiency, supporting conversion of more file formats, etc. I hope this article can provide you with useful references to help you successfully achieve batch extraction of PDF text content.

Related Q&A FAQs

How to choose the right library to extract PDF text content?In Python, there are multiple libraries that can be used to extract PDF text content, the most commonly used ones include PyPDF2, pdfminer, and PyMuPDF. Choosing the right library depends on your needs. If you need simple text extraction, PyPDF2 may be enough. But if more complex processing is required, such as retaining text formats or extracting specific elements, pdfminer or PyMuPDF will be more appropriate.

What are the common problems in the process of extracting text?When extracting PDF text in batches, users may encounter some problems, such as encryption protection of PDF files, loss of text format, or garbled text extracted. To solve these problems, make sure the libraries used support the processing of encrypted files and consider using OCR techniques such as Tesseract to process scanned PDF files.

How to deal with extracted text data?Once the text is successfully extracted, further analysis and processing can be performed using Python's data processing library such as Pandas. You can save the extracted text as a CSV file to facilitate subsequent data analysis, or use regular expressions to clean and format the text to extract useful information.

The above is the detailed content of how Python extracts PDF text content in batches. For more information about Python extracting PDF text, please follow my other related articles!