Python中用PyPDF2拆分pdf提取页面的方法-创新互联

Python中用PyPDF2拆分pdf提取页面的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

创新互联是一家集网站建设,南靖企业网站建设,南靖品牌网站建设,网站定制,南靖网站建设报价,网络营销,网络优化,南靖网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

准备工作:

安装扩展库PyPDF2,参考命令

pip install PyPDF2

代码如下:

from PyPDF2 import PdfFileReader, PdfFileWriter def split_pdf(filename, result, start=0, end=None):    """从filename中提取[start,end)之间的页码内容保存为result"""    # 打开原始 pdf 文件    pdf_src = PdfFileReader(filename)    if end is None:        # 获取页数        end = pdf_src.getNumPages()    with open(result, "wb") as fp:        # 创建空白pdf文件        pdf = PdfFileWriter()        # 提取页面内容,写入空白文件        for num in range(start, end):            pdf.addPage(pdf_src.getPage(num))        # 写入结果pdf        pdf.write(fp) fn = r"G:\a001\第九天.pdf" split_pdf(fn, "1.pdf", 0, 3) split_pdf(fn, "2.pdf", 1, 3) split_pdf(fn, "3.pdf", 2, 3)

遇见的问题一:

Traceback (most recent call last):  File "G:/a001/pdf.py", line 22, insplit_pdf(fn, "1.pdf", 0, 3)  File "G:/a001/pdf.py", line 7, in split_pdf    pdf_src = PdfFileReader(filename)  File "E:\project_luffy\luffy\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__    self.read(stream)  File "E:\project_luffy\luffy\lib\site-packages\PyPDF2\pdf.py", line 1901, in read    raise utils.PdfReadError("Could not find xref table at specified location") PyPDF2.utils.PdfReadError: Could not find xref table at specified location

还没有找到好的解决问题的办法,但是我在操作过程中换了一个新的pdf文件就成功了,猜测是你的pdf文件出了问题。

遇见的问题二:

在解决了上面的问题之后,程序可以正常的使用,但是还会出一个问题:

PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]

虽然不影响,但是体验不好啊 ,继续解决吧

import sys if not sys.warnoptions:    import warnings    warnings.simplefilter("ignore")

上面代码要加在最上面

感谢各位的阅读!看完上述内容,你们对Python中用PyPDF2拆分pdf提取页面的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联-成都网站建设公司行业资讯频道。


网页题目:Python中用PyPDF2拆分pdf提取页面的方法-创新互联
URL地址:http://myzitong.com/article/dcjgco.html