# -- coding: utf-8 --**
import PyPDF2
# 打开原始PDF文件
# with open('zhongguojinxiandaishi.pdf', 'rb') as pdf_file:
# pdf_reader = PyPDF2.PdfReader(pdf_file)
# num_pages = len(pdf_reader.pages)
#
# # 确定分割点(例如,将页面一分为二)
# split_point = num_pages // 2
#
# # 创建两个PDF写入对象
# pdf_writer1 = PyPDF2.PdfWriter()
# pdf_writer2 = PyPDF2.PdfWriter()
#
# # 将页面分别添加到两个写入对象中
# for page_number in range(num_pages):
# if page_number < split_point:
# pdf_writer1.add_page(pdf_reader.pages[page_number])
# else:
# pdf_writer2.add_page(pdf_reader.pages[page_number])
#
# # 将写入对象内容保存到两个新的PDF文件中
# with open('第一部分.pdf', 'wb',encoding='utf-8') as output_file1:
# pdf_writer1.write(output_file1)
#
# with open('第二部分.pdf', 'wb',encoding='utf-8') as output_file2:
# pdf_writer2.write(output_file2)
import PyPDF2
# 打开原始PDF文件
with open('caozuoxitong.pdf', 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
# 确定分割点(例如,将页面一分为二)
split_point = num_pages // 2
# 创建两个PDF写入对象
pdf_writer1 = PyPDF2.PdfWriter()
pdf_writer2 = PyPDF2.PdfWriter()
# 将页面分别添加到两个写入对象中
for page_number in range(num_pages):
if page_number < split_point:
pdf_writer1.add_page(pdf_reader.pages[page_number])
else:
pdf_writer2.add_page(pdf_reader.pages[page_number])
# 将写入对象内容保存到两个新的PDF文件中
with open('caozuoxitong1.pdf', 'wb') as output_file1:
pdf_writer1.write(output_file1)
with open('caozuoxitong2.pdf', 'wb') as output_file2:
pdf_writer2.write(output_file2)
文章来源地址https://www.toymoban.com/news/detail-667513.html
文章来源:https://www.toymoban.com/news/detail-667513.html
到了这里,关于一个pdf文件分割成两个的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!