Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/2176fff12168dd69c575e71ff76fa678 to your computer and use it in GitHub Desktop.
Save documentprocessing/2176fff12168dd69c575e71ff76fa678 to your computer and use it in GitHub Desktop.
Learn to combine or join multiple PDFs into one, split a PDF into multiple PDFs, rotate and delete PDF pages in Python using PyMuPDF library. Check https://products.documentprocessing.com/merger/python/pymupdf/ for more details.
# Import PyMuPDF
import fitz
# Open first document
doc1 = fitz.open("documentprocessing.pdf")
# Open second document
doc2 = fitz.open("data.pdf")
# Append document 2 after document 1
doc1.insert_pdf(doc2)
# Save the new combined document
doc1.save("joined-doc1-and-doc2.pdf")
# Import PyMuPDF
import fitz
# Open a PDF file
doc = fitz.open("documentprocessing.pdf")
# Delete the second page
doc.delete_page(1)
# Save the modified PDF to a new file
output_file = "modified.pdf"
doc.save(output_file)
# Close the PDF document
doc.close()
# Import PyMuPDF
import fitz
# Open a PDF file
doc = fitz.open("documentprocessing.pdf")
# Rotate the first page clockwise by 90 degrees
first_page = doc[0]
first_page.set_rotation(90)
# Save the modified PDF to a new file
output_file = "modified.pdf"
doc.save(output_file)
# Close the PDF document
doc.close()
# Import PyMuPDF
import fitz
# Open the first PDF document
doc1 = fitz.open("joined-doc1-and-doc2.pdf")
# Create a new empty PDF document
doc2 = fitz.open()
# Insert the first 2 pages of doc1 into doc2
doc2.insert_pdf(doc1, to_page=1)
# Save the modified document as "first-and-last-10.pdf"
doc2.save("prev-doc1.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment