File tree 1 file changed +13
-3
lines changed
1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 1
1
from sys import argv
2
2
from PyPDF2 import PdfMerger , PdfFileReader
3
3
import sys
4
+ import shutil , os
4
5
5
6
def add_pages (input_pdf_path : str , template_pdf_path : str ):
6
7
input_pdf = open (input_pdf_path , "rb" )
@@ -17,12 +18,21 @@ def add_pages(input_pdf_path: str, template_pdf_path: str):
17
18
18
19
template .close ()
19
20
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 )
21
23
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 :
24
27
merger .write (out )
25
28
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 )
26
36
27
37
def main ():
28
38
You can’t perform that action at this time.
0 commit comments