45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import subprocess
|
|
import PyPDF2
|
|
import os
|
|
import shutil
|
|
|
|
def clear_workspace(files):
|
|
if not files or not isinstance(files, list) or len(files) == 0:
|
|
return
|
|
|
|
for file in files:
|
|
subprocess.run(["rm", file])
|
|
|
|
|
|
def merge_pdfs(pdf1_path, pdf2_path, output_path):
|
|
# open the two PDF files
|
|
with open(pdf1_path, 'rb') as file1, open(pdf2_path, 'rb') as file2:
|
|
# create PDF-Reader-Objects
|
|
pdf1_reader = PyPDF2.PdfReader(file1)
|
|
pdf2_reader = PyPDF2.PdfReader(file2)
|
|
|
|
# create PDF-Writer-Object
|
|
pdf_writer = PyPDF2.PdfWriter()
|
|
|
|
# add pages from the first PDF
|
|
for page_num in range(len(pdf1_reader.pages)):
|
|
page = pdf1_reader.pages[page_num]
|
|
pdf_writer.add_page(page)
|
|
|
|
# add pages from the second PDF
|
|
for page_num in range(len(pdf2_reader.pages)):
|
|
page = pdf2_reader.pages[page_num]
|
|
pdf_writer.add_page(page)
|
|
|
|
# save the combined PDF
|
|
with open(output_path, 'wb') as output_file:
|
|
pdf_writer.write(output_file)
|
|
|
|
|
|
# If a script failes the files are moved to the old_files directory so we need to move them back so that the paths defined in the files are correct
|
|
def move_files_back_from_old_files():
|
|
# check if old_files directory exists and move files back
|
|
if os.path.exists("oldFiles"):
|
|
for file in os.listdir("oldFiles"):
|
|
shutil.move(os.path.join("oldFiles", file), file)
|
|
os.rmdir("oldFiles") |