使用PyPDF4库对pdf文件进行批量拆分。需要指定正确路径,代码会将该路径下的所有pdf文件按指定的起、止页进行批量拆分,并保存为新的PDF文件。
安装库:
pip install PyPDF4
批量拆分代码:
import os
from PyPDF4 import PdfFileReader, PdfFileWriter
def split_pdf(input_path, start_page, end_page):
pdf = PdfFileReader(input_path)
start_page = max(0, start_page - 1)
end_page = min(end_page, pdf.getNumPages())
output = PdfFileWriter()
for page in range(start_page, end_page):
output.addPage(pdf.getPage(page))
# 指定拆分后的输出文件名
filename = os.path.splitext(os.path.basename(input_path))[0]
output_filename = f"{filename}_split_{start_page+1}_to_{end_page}.pdf"
with open(output_filename, "wb") as output_file:
output.write(output_file)
print(f"拆分成功!已保存为 {output_filename}")
# 遍历文件夹下的所有PDF文件
folder_path = "C:/Users/Administrator/Desktop" # 文件夹路径,可自行修改
for filename in os.listdir(folder_path):
if filename.endswith(".pdf"):
pdf_path = os.path.join(folder_path, filename)
split_pdf(pdf_path, 1, 3) # 提取前3页并保存,可自行修改