合并两个PDF文件的Python代码如下(或者调用Guan软件包:https://py.guanjihuan.com):
"""
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 combined_file:
output_pdf.write(combined_file)
# import guan
# guan.combine_two_pdf_files(input_file_1='a.pdf', input_file_2='b.pdf', output_file='combined_file.pdf')
【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com】