1. pdf操作
1.1 pdf分割
- 安装环境
pip install PyPDF4
- 示例代码
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))
# 指定拆分后的输出文件名
output_filename = f"output_{start_page+1}_to_{end_page}.pdf"
with open(output_filename, "wb") as output_file:
output.write(output_file)
print(f"拆分成功!已保存为 {output_filename}")
split_pdf("xxx.pdf", 1, 2)
2. pdf转换
2.1 pdf转word
- 安装库
pip install pdf2docx
- 示例代码
from pdf2docx import Converter
a = Converter(output_filename)
a.convert(f'output_{start_page+1}_to_{end_page}.docx')