From 3b318149af5817854b87cbd2de579e4681b907b2 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Wed, 28 Jun 2023 16:54:05 +0800 Subject: [PATCH] Create combine_two_pdf_files.py --- .../combine_two_pdf_files.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 language_learning/2023.06.28_combine_two_pdf_files/combine_two_pdf_files.py diff --git a/language_learning/2023.06.28_combine_two_pdf_files/combine_two_pdf_files.py b/language_learning/2023.06.28_combine_two_pdf_files/combine_two_pdf_files.py new file mode 100644 index 0000000..32411c9 --- /dev/null +++ b/language_learning/2023.06.28_combine_two_pdf_files/combine_two_pdf_files.py @@ -0,0 +1,34 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/34649 +""" + +import PyPDF2 + +# 创建一个空的PDF对象 +output_pdf = PyPDF2.PdfWriter() + +# 打开第一个PDF文件 +with open('a.pdf', 'rb') as file1: + pdf1 = PyPDF2.PdfReader(file1) + + # 将第一个PDF文件的所有页面添加到输出PDF对象中 + for page in range(len(pdf1.pages)): + output_pdf.add_page(pdf1.pages[page]) + +# 打开第二个PDF文件 +with open('b.pdf', 'rb') as file2: + pdf2 = PyPDF2.PdfReader(file2) + + # 将第二个PDF文件的所有页面添加到输出PDF对象中 + for page in range(len(pdf2.pages)): + output_pdf.add_page(pdf2.pages[page]) + +# 保存合并后的PDF文件 +with open('combined_file.pdf', 'wb') as merged_file: + output_pdf.write(merged_file) + + + +# import guan +# guan.combine_two_pdf_files(input_file1='a.pdf', input_file2='b.pdf', output_file='combined_file.pdf') \ No newline at end of file