Home > Backend Development > Python Tutorial > How to Work With PDF Documents Using Python

How to Work With PDF Documents Using Python

Joseph Gordon-Levitt
Release: 2025-03-02 09:54:11
Original
506 people have browsed it

How to Work With PDF Documents Using Python

PDF files are popular for their cross-platform compatibility, with content and layout consistent across operating systems, reading devices and software. However, unlike Python processing plain text files, PDF files are binary files with more complex structures and contain elements such as fonts, colors, and images.

Luckily, it is not difficult to process PDF files with Python's external modules. This article will use the PyPDF2 module to demonstrate how to open a PDF file, print a page, and extract text. For the creation and editing of PDF files, please refer to another tutorial from me.

Preparation

The core lies in using external module PyPDF2. First, install it using pip:

pip is a package management system for Python that installs and manages Python packages, and many packages are found in the Python Package Index (PyPI).

If you downloaded Python from python.org, pip is likely to be installed automatically. Enter the following command in the terminal to install PyPDF2:

pip install PyPDF2
Copy after login

To use all the features of PyPDF2 (including encryption, decryption and image processing), you can use the following command:

pip install PyPDF2[full]
Copy after login

If you only need AES encryption/decryption function, you can use:

pip install PyPDF2[crypto]
Copy after login

PyPDF2 supports RC4 encryption by default.

PyPDF2 Basics

PyPDF2 is a free open source library that supports the reading, writing, segmentation and merging of PDF files. This tutorial uses PyPDF2 version 2.11.1.

Read PDF file

We will use the Beauty and the Beast PDF version on Project Gutenberg as the sample file. You can download the file or use any other PDF file.

The following code demonstrates how to open and read a PDF file:

import PyPDF2

with open('beauty-and-the-beast.pdf', 'rb') as book:
    book_reader = PyPDF2.PdfReader(book)
Copy after login

The first line imports the PyPDF2 module. The PdfReader class is used to read a PDF file and represent its page as a Page object.

Get the number of pages:

import PyPDF2

with open('beauty-and-the-beast.pdf', 'rb') as book:
    book_reader = PyPDF2.PdfReader(book)
    number_of_pages = len(book_reader.pages)
    print(number_of_pages)  # 输出:48
Copy after login

Direct access to page number

get_page_number() Method to get the page number:

import random
from PyPDF2 import PdfReader

with open('beauty-and-the-beast.pdf', 'rb') as book:
    book_reader = PdfReader(book)
    page_list = book_reader.pages
    last_page = page_list[-1]
    print(book_reader.get_page_number(last_page))  # 输出:47 (实际为第48页)
    some_page = page_list[random.randint(15, 35)]
    print(book_reader.get_page_number(some_page))  # 输出:随机页码
Copy after login

Page mode and page layout

The

page_mode and page_layout properties return page mode and page layout information respectively:

from PyPDF2 import PdfReader

with open('beauty-and-the-beast.pdf', 'rb') as book:
    book_reader = PdfReader(book)
    print(book_reader.page_mode)  # 输出:None
    print(book_reader.page_layout)  # 输出:None
Copy after login

metadata Properties return metadata of PDF files, such as author, title, creation time, and generator, etc.:

from PyPDF2 import PdfReader

with open('beauty-and-the-beast.pdf', 'rb') as book:
    book_reader = PdfReader(book)
    book_metadata = book_reader.metadata
    print(book_metadata.title)       # 输出:Beauty and the Beast
    print(book_metadata.author)      # 输出:Anonymous
    print(book_metadata.creation_date) # 输出:例如 2006-11-30 01:13:00-08:00
    print(book_metadata.producer)    # 输出:例如 pdfeTeX-1.21a
Copy after login

Summary

Python simplifies the processing of PDF files through the PyPDF2 module. This article only introduces some of the functions of PyPDF2. For more details, please refer to the official PyPDF2 documentation.

The above is the detailed content of How to Work With PDF Documents Using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template