PDF Automation with Python: Merge, Split and Extract Text in One Script

Nine out of ten office workers have wrestled with PDFs: merging several quotes into one file, splitting a whole catalogue into single pages, or copying numbers out of a hundred-page report by hand. Doing this manually is slow — and worse, it's error-prone: you merge the wrong file, skip a page, or copy a figure incorrectly. And these tasks tend to be routine, repeating every single week and burning time. Today I'll show you how to automate it all with Python and a free library called pypdf, in just a few lines of code — once and for all.

Step 1: Install pypdf

Open Terminal (Mac) or Command Prompt (Windows) and run:

pip install pypdf

Once installed, you're ready to write your script. The three tricks below cover most office PDF tasks — each one is under a dozen lines.

Why pypdf?

pypdf is a PDF toolkit written in pure Python — free, no extra dependencies, and installs in seconds. There are other options like pdfplumber (great at extracting tables) and PyMuPDF (fastest), but for the three most common paperwork tasks — merge, split and extract text — pypdf is more than enough, and it's the simplest to learn. A beginner can pick it up in a day.

Trick 1: Merge Multiple PDFs

The most common need: combine several separate PDFs into one, so you can send it to a client or archive it in one go. Use PdfWriter's append() to add each file and then write the result:

from pypdf import PdfReader, PdfWriter # Merge multiple PDFs into one merger = PdfWriter() for f in ["quote.pdf", "terms.pdf", "drawing.pdf"]: merger.append(f) merger.write("merged.pdf") merger.close()

Put the files you want to merge into the list — their order in the list is the order in the merged result. Add another file? Just add another line to the list, no other code changes needed. If your set of files is always the same (e.g. quote + terms + drawing), save the code as a .py file and run it once each time to merge automatically.

Trick 2: Split a PDF into Single Pages

Your monthly statement is dozens of pages and you want each page as its own file? Read it with PdfReader, then write each page out:

reader = PdfReader("big.pdf") for i, page in enumerate(reader.pages): w = PdfWriter() w.add_page(page) w.write(f"page_{i+1}.pdf") w.close()

enumerate() counts the pages for you, and i+1 is the page number. A hundred pages becomes a hundred files in seconds — no more "save as" page by page. If you only want a few pages, change the for to for i in [0, 2, 5] to output just pages 1, 3 and 6.

Trick 3: Extract Text from a PDF

Need to copy data from a PDF into Excel? Don't type it out line by line. Use extract_text() to pull all the text out:

reader = PdfReader("document.pdf") for i, page in enumerate(reader.pages): print(f"--- Page {i+1} ---") print(page.extract_text())

The extracted text is plain text, so you can tidy it up with the Python data-cleaning techniques we covered earlier (strip spaces, remove duplicates) before loading it into Excel — automating the whole pipeline. Note: if the PDF is a scan (each page is just an image), extract_text() will return nothing — those need an OCR tool instead.

Bonus: Rotate and Encrypt

Beyond the three tricks, two more features come in handy — straightening landscape pages and encrypting business documents:

reader = PdfReader("document.pdf") w = PdfWriter() for page in reader.pages: w.add_page(page) w.pages[-1].rotate(90) # rotate each page 90 degrees clockwise w.encrypt("secret123") # encrypt the output w.write("output.pdf") w.close()

rotate() fixes pages that came out sideways, and encrypt() adds password protection — a lot more reassuring when sending sensitive material to a client.

Tips

  • Order follows the list: when merging, whichever file comes first in the list comes first in the output — no need to re-order.
  • Remember close(): always call close() after writing, otherwise the file stays locked and won't open.
  • Chinese filenames work: pypdf supports Chinese paths and filenames — use them as-is.
  • Scans need OCR: image-only PDFs can't be extracted as text — use an OCR tool to convert them first.

This Python tutorial summary: With these three tricks plus two bonus features, most PDF paperwork can be automated. Give it a try — the time you save is yours to keep.