Skip to content

Commit a78551d

Browse files
committed
new file will be named automatically to original while original file will become backup
1 parent 88dca69 commit a78551d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

pdfs/add_comment_page.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from sys import argv
22
from PyPDF2 import PdfMerger, PdfFileReader
33
import sys
4+
import shutil, os
45

56
def add_pages(input_pdf_path: str, template_pdf_path: str):
67
input_pdf = open(input_pdf_path, "rb")
@@ -17,12 +18,21 @@ def add_pages(input_pdf_path: str, template_pdf_path: str):
1718

1819
template.close()
1920
input_pdf.close()
20-
write_file(input_pdf_path, merger)
21+
output_filepath = write_file(input_pdf_path, merger)
22+
rename_file(input_pdf_path, output_filepath)
2123

22-
def write_file(input_pdf_path: str, merger: PdfMerger):
23-
with open(input_pdf_path+"merged.pdf", "wb") as out:
24+
def write_file(input_pdf_path: str, merger: PdfMerger)->str:
25+
output_file_path = input_pdf_path+"merged.pdf"
26+
with open(output_file_path, "wb") as out:
2427
merger.write(out)
2528
merger.close()
29+
return output_file_path
30+
31+
def rename_file(input_filepath, output_filepath):
32+
input_filename, ext = os.path.splitext(input_filepath)
33+
backup_filepath = input_filename+"_backup"+ext
34+
os.rename(input_filepath, backup_filepath)
35+
os.rename(output_filepath, input_filepath)
2636

2737
def main():
2838

0 commit comments

Comments
 (0)