Last active
September 28, 2024 12:00
-
-
Save BennoCrafter/61fd6fb724a998a40855f698ce18ac96 to your computer and use it in GitHub Desktop.
Merge and rotate PDF files from the command line
This file contains 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
""" | |
Usage: | |
python3 main.py --res <resources_folder> --out <output_folder> | |
Merge and rotate PDF files from the command line | |
Options: | |
--res <resources_folder> Path to the folder containing resource directories with PDF files. | |
--out <output_folder> Path to the output folder where merged and rotated PDFs will be saved. | |
Requirements: | |
PyPDF2 | |
""" | |
import os | |
import argparse | |
import PyPDF2 | |
## Helper Functions ## | |
def merge_pdfs(pdf_path_list, output_path): | |
pdf_merger = PyPDF2.PdfMerger() | |
pdf_path_list.sort() | |
for pdf in pdf_path_list: | |
if not check_file_extension(pdf, ".pdf"): | |
print(f"Found invalid file: {pdf}. Skipping...") | |
continue | |
try: | |
with open(pdf, 'rb') as file: | |
pdf_merger.append(file) | |
except Exception as e: | |
print(f"Failed to merge {pdf}: {e}") | |
with open(f"{output_path}.pdf", 'wb') as output_file: | |
pdf_merger.write(output_file) | |
def rotate_pdf(input_path, output_path): | |
try: | |
with open(input_path, 'rb') as file: | |
reader = PyPDF2.PdfReader(file) | |
writer = PyPDF2.PdfWriter() | |
for index, page in enumerate(reader.pages): | |
rotated_page = page.rotate(90) if index % 2 == 0 else page.rotate(270) | |
writer.add_page(rotated_page) | |
with open(output_path, 'wb') as output_file: | |
writer.write(output_file) | |
except Exception as e: | |
print(f"Failed to rotate {input_path}: {e}") | |
def get_all_dirs(path) -> list: | |
return [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))] | |
def get_all_files(folder_path) -> list: | |
return [os.path.join(folder_path, f) for f in os.listdir(folder_path)] | |
def check_file_extension(file_path, extension): | |
_, file_ext = os.path.splitext(file_path) | |
return file_ext.lower() == extension.lower() | |
def main(res_folder_path, output_folder_path): | |
# read res dir and folders | |
folders = get_all_dirs(res_folder_path) | |
# create output folder if it doesn't exist | |
os.makedirs(output_folder_path, exist_ok=True) | |
for folder_name in folders: | |
folder_path = os.path.join(res_folder_path, folder_name) | |
pdf_files = get_all_files(folder_path) | |
# Merge PDFs from the folder | |
merge_pdfs(pdf_files, os.path.join(output_folder_path, folder_name)) | |
# Rotate the merged PDF | |
rotate_pdf(input_path=os.path.join(output_folder_path, f"{folder_name}.pdf"), | |
output_path=os.path.join(output_folder_path, f"{folder_name}.pdf")) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Merge and rotate PDF files from the command line") | |
parser.add_argument('--res', type=str, required=True, help="Path to the resources folder.") | |
parser.add_argument('--out', type=str, required=True, help="Path to the output folder.") | |
args = parser.parse_args() | |
main(args.res, args.out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment