Working with PDF Files in Python

Table of Contents

    Portable Document Format (PDF) is a common file format for digital documents including text, images and more. Python can work with PDFs to extract information, split, merge and secure these files. This tutorial will cover the main ways to handle PDFs in Python.

    Python Packages for PDFs

    There are several excellent Python packages available for working with PDF files:

    • PyPDF2 - This is the core PDF library. It can read, split, merge, crop, and transform PDF pages. It also supports encryption.
    • pdfminer - For extracting text and formatting from PDFs. It includes text extraction capabilities.
    • ReportLab - Feature-rich PDF generation library. It can create complex PDF reports from scratch in Python.
    • pdfrw - Read, write, fill forms and other common PDF transformations. Good for merging PDFs.
    • Camelot - Extract tables and tabular data from PDFs into Pandas dataframes.
    • Tika - General text and metadata extraction from many file formats including PDF.
    • Wand - Bindings for ImageMagick and MagickWand C libraries for image processing including PDFs.

    The PyPDF2 covers most common PDF operations while libraries like pdfminer, reportlab and camelot provide more specialized capabilities. Select your package based on your specific needs.

    Reading PDF Contents

    The PyPDF2 module can read and extract text and metadata from PDFs:

    import PyPDF2
    pdf = PyPDF2.PdfFileReader('file.pdf')
    page = pdf.getPage(0)

    Use PdfFileReader to read PDF files. The pages can be accessed and text extracted with getPage() and extractText() methods.

    Splitting and Merging PDFs

    PyPDF2 can also split or merge PDFs:

    pdf = PyPDF2.PdfFileReader('large_file.pdf')
    writer = PyPDF2.PdfFileWriter()

    for i in range(pdf.getNumPages()): p = pdf.getPage(i) writer.addPage(p)

    with open('new_file.pdf', 'wb') as f: writer.write(f)

    The PdfFileWriter can output pages from a PdfFileReader into a new PDF. This allows splitting or merging files.

    Encrypting and Decrypting

    To add a password protection when writing:


    And to decrypt when reading:

    pdf = PyPDF2.PdfFileReader('protected.pdf')

    Extracting Text and Data

    In addition to extractText(), text and data can be extracted with:

    • pdfminer - for text extraction
    • Camelot - for tabular data extraction
    • Tika - to detect text, metadata, annotations

    Creating and Editing PDFs

    The ReportLab library can generate PDFs:

    from reportlab.pdfgen import canvas

    canvas = canvas.Canvas("new.pdf") canvas.drawString(100, 100, "Hello World") canvas.save()


    In summary, Python has great libraries like PyPDF2 for working with PDF files. It can extract text and metadata, split and merge files, add encryption and passwords, extract tabular data, detect annotations and more. With Camelot and ReportLab, new PDF files can even be created from scratch. By leveraging these Python libraries, PDFs can be parsed, manipulated and generated for any application.

    Andrew ThompsonAndrew Thompson
    I have over 10 years of experience programming in Python. I am skilled in web development with Django and Flask, data analysis with Pandas and NumPy, and scientific computing with SciPy. I am also proficient at Python automation and scripting.