一个PDF文档很长,希望按照章节分拆成小文档。
可以在kimichat中输入提示词:
你是一个Python编程专家,要完成一个编写拆分PDF文档的Python脚本的任务,具体步骤如下:
打开文件夹:D:\chatgpt图书\图书1,
读取文件夹里面的PDF文档:Porter L. Learn AI-assisted Python Programming;
这个PDF文档里面一共有11个章节,需要拆分出来的章节是这6、7、8、9这三个章节:
第6章Testing and prompt engineering,页码范围是:99—123
第7章 Problem decomposition,页码范围是:124—157
第8章Debugging and better understanding your code,页码范围是:158—181
第9章Automating tedious tasks,页码范围是:182—215
用PyPDF2库将第7章、第8章、第9章分拆成3个独立的PDF文档,文档标题名和章节标题名一致,保存在文件夹:打开文件夹:D:\chatgpt图书\图书分拆;
然后用python-docx库将PDF文档转换成docx文档格式;
注意:
每一步都要打印相关的信息;
要有错误处理和调试信息,这有助于找出问题所在;
PdfFileReader 类在 PyPDF2 版本 3.0.0 中已经被弃用并移除了,应该使用 PdfReader 类来代替。
这是kimi智能助手生成的代码:
import os
import PyPDF2
from PyPDF2 import PdfReader, PdfWriter
from pdf2docx import Converter
pdf_folder_path = "D:\chatgpt图书\图书1"
output_folder_path = "D:\chatgpt图书\图书分拆"
pdf_filename = "Porter L. Learn AI-assisted Python Programming.pdf"
chapters = {
"Problem decomposition": (124, 157),
"Debugging and better understanding your code": (158, 181),
"Automating tedious tasks": (182, 215)
}
if not os.path.exists(pdf_folder_path):
print(f"Error: The folder {pdf_folder_path} does not exist.")
exit()
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
pdf_path = os.path.join(pdf_folder_path, pdf_filename)
if not os.path.exists(pdf_path):
print(f"Error: The file {pdf_filename} does not exist in the folder {pdf_folder_path}.")
exit()
pdf_reader = PdfReader(pdf_path) # 使用PdfReader替代PdfFileReader
num_pages = len(pdf_reader.pages)
print(f"The PDF document has {num_pages} pages.")
for chapter_title, (start_page, end_page) in chapters.items():
try:
pdf_writer = PdfWriter()
for page_num in range(start_page, end_page + 1):
if page_num <= num_pages:
pdf_writer.add_page(pdf_reader.pages[page_num - 1]) # PdfReader使用pages属性
else:
print(f"Warning: Page {page_num} does not exist in the document.")
pdf_writer.add_metadata({
'/Title': chapter_title,
'/Author': 'Porter L.'
})
output_pdf_path = os.path.join(output_folder_path, f"{chapter_title}.pdf")
pdf_writer.write(open(output_pdf_path, 'wb'))
print(f"Chapter '{chapter_title}' has been successfully saved as {output_pdf_path}")
cv = Converter(output_pdf_path)
docx_path = os.path.splitext(output_pdf_path)[0] + ".docx"
cv.convert(docx_path, start=0, end=None)
cv.close()
print(f"Chapter '{chapter_title}' has been successfully converted to docx at {docx_path}")
except Exception as e:
print(f"An error occurred while processing chapter '{chapter_title}': {e}")
print("PDF splitting and conversion process completed.")
运行程序,成功分拆文档: