To study the basic grammar of Python, I wrote a program to combine multiple PDF files into one PDF file. PyPDF2 is used to combine and write PDF files.
Install PyPDF2 using pip.
> pip install PyPDF2
Import PdfFileMerger
to integrate the PDF files, and ʻos,
glob` to automatically find the PDF files when you specify the folder.
merge.py(1)
from PyPDF2 import PdfFileMerger
import os
import glob
Initialize for integration.
merge.py(2)
def main():
merger = PdfFileMerger()
merge_files = []
First, continue specifying until m
is entered, and if m
is entered, finish specifying the file and proceed to the next process. The variable i is for counting the number of files.
merge.py(3)
i = 1
while True:
print("Merge file or Folder ", i, " (Type 'm' to merge.) -> ", sep='', end='')
in_file = input()
if in_file == 'm':
break
If the entered path is a file name and it is a PDF file, it will be added to the file list to be merged. If the extension is not PDF, a message is output to convey it.
merge.py(4)
elif os.path.isfile(in_file):
ext = os.path.splitext(in_file)
if ext == '.pdf':
merge_files.append(in_file)
i += 1
else:
print("The specified file is not a PDF file.")
Here, we first identify whether the argument is a file or a folder by ʻos.path.isfile (). Then we use ʻos.path.splitext ()
to identify the extension.
If the specified file is a PDF file, ʻappend ()` adds it to the list of files to merge.
If the entered path is a folder name, all pdf files under that directory are added to the list and the added file name is output.
merge.py(5)
else:
for file in glob.glob(in_file + '*.pdf'):
merge_files.append(file)
print("Add " + file)
i += 1
The PDF files in the specified folder are listed by glob.glob ()
and added to the list to be merged in order.
Specify the name of the PDF file to output. If the extension of the specified name is not .pdf
, it is converted to PDF format by adding .pdf
at the end.
merge.py(6)
print("Generated file -> ", end='')
out_file = input()
ext = os.path.splitext(out_file)
if ext != '.pdf':
out_file = out_file + '.pdf'
Again, we use ʻos.path.splitext ()` to look up the extension.
The file to be integrated is added to merger and then written to the specified file.
merge.py(7)
for file in merge_files:
merger.append(file)
merger.write(out_file)
merger.close()
print("File merge completed!!")
Add it to the merger with ʻappend ()and then create a PDF file at the specified output destination with
write ()`.
Please check the entire code from github → (PDF-Handler) Please note that Python is a beginner, so it may be difficult to read.
Recommended Posts