Python: Extract Text from a PDF
Read the text out of any PDF with Python — the first step for search, summaries and automation.
Merge several PDF files into one with Python in a few lines — keep the page order you choose.
15 lines · Updated Nov 26, 2026
from pypdf import PdfWriter
def merge_pdfs(files, output):
writer = PdfWriter()
for path in files:
writer.append(path)
with open(output, "wb") as f:
writer.write(f)
print(f"Merged {len(files)} files into {output}")
if __name__ == "__main__":
merge_pdfs(
["chapter1.pdf", "chapter2.pdf", "chapter3.pdf"],
"complete-book.pdf",
)
Combining PDFs is a daily task in many offices — monthly invoices, scanned pages, report chapters — and online PDF tools mean uploading private documents to someone else's server. This snippet merges any number of PDFs into one file locally with Python and the pypdf library, in the order you choose.
pip install pypdf.01-cover.pdf, 02-report.pdf…).10.pdf sorts before 2.pdf as text — use zero-padded numbers.Is my data uploaded anywhere? No — everything runs on your own computer.
Related: Extract text from a PDF · 10 Python automation projects
Install with pip install pypdf. List the files in the order you want them merged, then run the script. For very large PDFs, merge in smaller batches.
Share this snippet
Read the text out of any PDF with Python — the first step for search, summaries and automation.
Send yourself a Telegram message from Python — free alerts for scripts, servers and price trackers.
Generate a strong random password with Python using the secrets module — never use random() for passwords.