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