insert_pdf is a PyMuPDF method for inserting pages from PDF documents into a new document, making PDF merging practical with Python. section 9.4.2
A basic merge workflow:
- Install PyMuPDF and import it as fitz. section 9.4.2
- Create a destination Document and open each input PDF. section 9.4.2
- Insert pages with insert_pdf(), then save and close the merged document. section 9.4.2
Merging two PDFs
Use file1.pdf and file2.pdf as inputs, insert their pages into pdf_writer, and save the result as merged.pdf. section 9.4.2
```pythonimport fitz# Merge the two PDF filespdf_files = ['file1.pdf', 'file2.pdf']pdf_writer = fitz.Document()for pdf_file in pdf_files:pdf_reader = fitz.open(pdf_file)for page in pdf_reader:pdf_writer.insert_pdf(page)pdf_writer.save('merged.pdf')pdf_writer.close()```
View the compact PyMuPDF example showing fitz import, input files, page insertion, saving, and closing. section 9.4.2
