提取PDF的特定页面并用Python保存

2024-10-01 11:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些来源,并试图代码提取一些网页和创建pdf文件。 我有一份清单,看起来像这样

information = [(filename1,startpage1,endpage1), (filename2, startpage2, endpage2), ...,(filename19,startpage19,endpage19)].

这是我的密码。在

^{pr2}$

但是输出很奇怪。。有的里面什么都没有,有的只有一页。我该怎么更正?在


Tags: 文件代码网页informationpdf来源filename1filename2
1条回答
网友
1楼 · 发布于 2024-10-01 11:31:48

我已经解决了这个问题。它是等号(start<;=结束)。在

for page in range(len(information)):
    pdf_writer = PyPDF2.PdfFileWriter()
    start = information[page][1]
    end = information[page][2]
    while start<=end:
        pdf_writer.addPage(pdfReader.getPage(start-1))
        start+=1
    if not os.path.exists(savepath):
        os.makedirs(savepath)
    output_filename = '{}_{}_page_{}.pdf'.format(information[page][0],information[page][1], information[page][2])
    with open(output_filename,'wb') as out:
        pdf_writer.write(out)

相关问题 更多 >