Skip to content

Instantly share code, notes, and snippets.

@anderl80
Last active December 7, 2021 18:06
Show Gist options
  • Save anderl80/456e2dd9a644aa380987869d96ed6b76 to your computer and use it in GitHub Desktop.
Save anderl80/456e2dd9a644aa380987869d96ed6b76 to your computer and use it in GitHub Desktop.
Merge separate scans of front and back pages to full duplex scan document.
# https://stackoverflow.com/questions/3444645/merge-pdf-files
import argparse
import warnings
from PyPDF2 import PdfFileWriter, PdfFileReader
parser = argparse.ArgumentParser()
parser.add_argument(dest='front', help="Scan containing front pages.")
parser.add_argument(dest='back', help="Scan containing back pages.")
parser.add_argument(dest='output', help="Output file.")
args = parser.parse_args()
def load_pdf(file_path):
"""Load and return PDF file object."""
pdf_file = PdfFileReader(open(file_path, 'rb'), strict=False)
return pdf_file
def sort_duplex_scan(front_pages, back_pages, output_pages):
"""Simulate duplex scan by sorting pages from two documents."""
if(front_pages.getNumPages() != back_pages.getNumPages()):
warnings.warn("Number of pages not matching!")
for i in range(front_pages.getNumPages()):
output_pages.addPage(front_pages.getPage(i))
output_pages.addPage(back_pages.getPage(back_pages.getNumPages()-i-1))
return output_pages
def save_pdf(output_pages, file_name):
"""Save merged PDF to given location."""
with open(file_name, 'wb') as f:
output_pages.write(f)
def main():
front = load_pdf(args.front)
back = load_pdf(args.back)
output = PdfFileWriter()
output = sort_duplex_scan(front, back, output)
save_pdf(output, args.output)
if __name__ == "__main__":
main()
# https://stackoverflow.com/questions/3444645/merge-pdf-files
import argparse
import warnings
from PyPDF2 import PdfFileWriter, PdfFileReader
import PySimpleGUI as sg
def load_pdf(file_path):
"""Load and return PDF file object."""
pdf_file = PdfFileReader(open(file_path, 'rb'), strict=False)
return pdf_file
def sort_duplex_scan(front_pages, back_pages, output_pages):
"""Simulate duplex scan by sorting pages from two documents."""
if(front_pages.getNumPages() != back_pages.getNumPages()):
warnings.warn("Number of pages not matching!")
for i in range(front_pages.getNumPages()):
output_pages.addPage(front_pages.getPage(i))
output_pages.addPage(back_pages.getPage(back_pages.getNumPages()-i-1))
return output_pages
def save_pdf(output_pages, file_name):
"""Save merged PDF to given location."""
with open(file_name, 'wb') as f:
output_pages.write(f)
def main():
layout = [[sg.T("Choose input and output location.")],
[sg.Text("Front pages PDF: "),
sg.FileBrowse(key="-FRONT-")],
[sg.Text("Back pages PDF: "),
sg.FileBrowse(key="-BACK-")],
[sg.Text("Output pages PDF: "),
sg.Input(key="-OUT-" , change_submits=True)],
[sg.Button("Submit")]]
window = sg.Window('PDF merger', layout, size=(600, 300))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event=="Exit":
break
elif event == "Submit":
print()
front = load_pdf(values["-FRONT-"])
back = load_pdf(values["-BACK-"])
output = PdfFileWriter()
output = sort_duplex_scan(front, back, output)
save_pdf(output, values["-OUT-"])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment