Created
May 10, 2024 07:29
-
-
Save Rgghgh/93b82b9a0bf724138f2ccac5ae00be3c to your computer and use it in GitHub Desktop.
PDF overlay merger (file 1 is the base, file 2 is the overlay)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fitz # Import PyMuPDF | |
| def overlay_notes(original_pdf_path, notes_pdf_path, output_pdf_path): | |
| # Open the original and notes documents | |
| original_pdf = fitz.open(original_pdf_path) | |
| notes_pdf = fitz.open(notes_pdf_path) | |
| # Iterate through the pages and overlay notes onto the original | |
| for page_number in range(len(original_pdf)): | |
| original_page = original_pdf[page_number] | |
| if page_number < len(notes_pdf): # Check if there's a corresponding notes page | |
| notes_page = notes_pdf[page_number] | |
| # Create a pixmap of the notes page with an alpha channel (transparency) | |
| pixmap = notes_page.get_pixmap(alpha=True) | |
| # Insert the pixmap onto the original page | |
| original_page.insert_image(original_page.rect, pixmap=pixmap) | |
| # Save the modified document to a new file | |
| original_pdf.save(output_pdf_path) | |
| # File paths | |
| original_pdf_path = '1.pdf' | |
| notes_pdf_path = '2.pdf' | |
| output_pdf_path = 'merged_output.pdf' | |
| # Function call to overlay the notes | |
| overlay_notes(original_pdf_path, notes_pdf_path, output_pdf_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment