一个pdf文件分割成两个

# -- 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)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值