• 技术文章 >Python技术 >Python基础教程

    Python中用PyPDF2拆分pdf提取页面

    2021-04-15 10:30:49原创4225
    有时候我们只需要pdf中的几页,或许还想把这几页内容整合成新的pdf,那该怎么做呢?

    准备工作:

    安装扩展库PyPDF2,参考命令

    1

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">pip install PyPDF2<br></span></p>

    代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">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)<br></span></p>

    遇见的问题一:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">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<br></span></p>

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

    遇见的问题二:

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

    1

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]<br></span></p>

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

    1

    2

    3

    4

    5

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">import sys

      

    if not sys.warnoptions:

        import warnings

        warnings.simplefilter("ignore")<br></span></p>

    上面代码要加在最上面

    关于PyPDF2的内容,昨天有详细讲解过,不会的小伙伴可以查看:进阶PDF,就用Python(PyPDF2模块)

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:pypdf2 pdf提取页面
    上一篇:了解搭建selenium安装以及配置环境 下一篇:python库在哪里下载?怎么安装?

    相关文章推荐

    • python中怎么将pdf内容显示出来?• pdf如何用python读取?• 进阶PDF,就用Python(pdfminer.six和pdfplumber模块)• Python如何读取pdf中的图片• Python教程:pdfplumber提取pdf中的表格数据• 如何用Python把pdf转换成word• Python中怎样把html网页转换成pdf

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网