Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/8e69bf4cb1db0ea2783fdf7e24e5c645 to your computer and use it in GitHub Desktop.
Save documentprocessing/8e69bf4cb1db0ea2783fdf7e24e5c645 to your computer and use it in GitHub Desktop.
Add, Rotate, Crop, Merge & Split PDF Files in Python using pypdf Library. Check https://products.documentprocessing.com/merger/python/pypdf/ for more details.
# Import the PdfWriter & PdfReader classes from the pypdf library
from pypdf import PdfWriter, PdfReader
# Open PDF document and instantiate writer object for performing operations on the PDF
reader = PdfReader("documentprocessing.pdf")
writer = PdfWriter()
# Add page 1 from reader to output document, unchanged:
writer.add_page(reader.pages[0])
# Add page 2 from reader, but rotated clockwise 90 degrees:
writer.add_page(reader.pages[1].rotate(90))
# Add page 3 from reader, but crop it to half size:
page3 = reader.pages[2]
page3.mediabox.upper_right = (
page3.mediabox.right / 2,
page3.mediabox.top / 2,
)
writer.add_page(page3)
# Add some Javascript to launch the print window on opening this PDF.
# The password dialog may prevent the print dialog from being shown,
# Comment the the encription lines, if that's the case, to try this out:
writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# Write to document-output.pdf
with open("pypdf-output.pdf", "wb") as fp:
writer.write(fp)
# Import the PdfWriter class from the pypdf library
from pypdf import PdfWriter
# Create a PdfWriter object to merge PDFs
merger = PdfWriter()
# List of PDF files to be merged
pdf_files = ["documentprocessing.pdf", "data.pdf"]
# Iterate through the PDF files and append them to the merger
for pdf in pdf_files:
merger.append(pdf)
# Write the merged PDF to a new file named "merged-pdf.pdf"
merger.write("merged-pdf.pdf")
# Close the merger object
merger.close()
# Import the PdfWriter & PdfReader classes from the pypdf library
from pypdf import PdfReader, PdfWriter
# Open the PDF Document
input_pdf = PdfReader(open("1.pdf", "rb"))
# Split the PDF into two parts
mid_page = int(len(input_pdf.pages) / 2)
first_half = PdfWriter()
second_half = PdfWriter()
for page_num in range(mid_page):
first_half.add_page(input_pdf.pages[page_num])
for page_num in range(mid_page, len(input_pdf.pages)):
second_half.add_page(input_pdf.pages[page_num])
# Save the two halves to the same directory
output_file1 = 'first_half.pdf'
output_file2 = 'second_half.pdf'
with open(output_file1, 'wb') as output1:
first_half.write(output1)
with open(output_file2, 'wb') as output2:
second_half.write(output2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment