跳转到内容

User:Xyy23330121/Python/pdf

来自维基学院


本章节将基于第三方库 PyPDF2 来讲述如何使用 Python 处理 PDF 文件。

安装[编辑 | 编辑源代码]

PyPDF2 需要大于 3.6 版本的 Python 才能正常运行。对于使用从 python.org 下载的 Windows 版本的 Python,我们可以从命令行程序中,输入以下代码以安装 PyPDF2。

python -m pip install PyPDF2

如果链接不畅,或者下载失败。可以用以下代码,改为从清华大学提供的托管服务器中下载 requests。

python -m pip install PyPDF2 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装可选依赖[编辑 | 编辑源代码]

PyPDF 的部分功能需要在安装一些额外的依赖项目才能使用。以下将列出这些功能,与安装依赖项目的方法。

图像提取依赖[编辑 | 编辑源代码]

如果希望提取 PDF 中的图片,则需要使用以下代码安装依赖项目:

python -m pip install PyPDF2[image]

PDF 加密依赖[编辑 | 编辑源代码]

如果希望使用 AES 加密或解密 PDF 文件,则需要使用以下代码安装依赖项目:

python -m pip install PyPDF2[crypto]

如果只需要使用 RC4 加密,则不需要此依赖项。

安装全部依赖项[编辑 | 编辑源代码]

可以使用以下代码直接安装全部依赖项:

python -m pip install PyPDF2[full]

提取 PDF 中的信息[编辑 | 编辑源代码]

导入 pdf 文件[编辑 | 编辑源代码]

PyPDF2 使用 PdfReader 类来读取 pdf 文件。使用示例如下:

from PyPDF2 import PdfReader

reader = PdfReader("example.pdf") #输入pdf文件所对应的路径,读取该pdf文件

PdfReader 在创建时有三个参数:

  • stream:表示需要处理的 pdf 文件。这里可以是经过内置 open() 函数打开的支持读取的文件对象,也可以是以字符串形式或 os.PathLike 形式的路径。
  • strict:(默认为 False)如果为 True,则在读取 pdf 文件中产生的任何问题都会报错。
  • password:(默认为 None)如果 pdf 文件具有密码,输入密码以在导入文件时进行解密。密码可以为字符串或 bytes 类型。如果不输入密码,则不会进行解密。

提取元信息[编辑 | 编辑源代码]

在导入文件后,我们可以提取 pdf 文件的元信息。

from PyPDF2 import PdfReader
reader = PdfReader("example.pdf")

meta = reader.metadata            #读取元信息
print(len(reader.pages))          #页数
print(meta.author)                #作者(若元信息中没有记录作者,则为空)
print(meta.creator)               #文件创建者(若元信息中不包含创建者,则为空)
print(meta.producer)              #文件提供者(若元信息中不包含提供者,则为空)
print(meta.subject)               #主题(若元信息中不包含主题,则为空)
print(meta.title)                 #标题(若元信息中不包含标题,则为空)

提取文本信息[编辑 | 编辑源代码]

通过以下方法,还可以提取所选择页内的文字。

from PyPDF2 import PdfReader
reader = PdfReader("example.pdf")

page = reader.pages[0]        #选择第 1 页。
print(page.extract_text())    #提取第 1 页的所有文字,以文本形式输出。

在提取时,还可以添加以下参数来筛选提取的文字信息。这些参数必须 按关键字传入

  • orientations:以整数角度筛选文字朝向,0 为向上,90 为向左,180 为颠倒,270 为向右。可以传入包含多个角度的整数数组以同时筛选多个朝向。默认为 (0, 90, 180, 270),即不筛选。
  • space_width:空格宽度(默认为200.0)
  • visitor_opperand_before:参见文档
  • visitor_opperand_after:参见文档
  • visitor_text:参见文档

提取图片[编辑 | 编辑源代码]

可以用以下方式提取指定页的图片。

from PyPDF2 import PdfReader
reader = PdfReader("example.pdf")

page = reader.pages[0]
count = 0

for image_file_object in page.images:
    with open(str(count) + image_file_object.name, "wb") as fp:
        fp.write(image_file_object.data)
        count += 1

PDF 加密与解密 =[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/encryption-decryption.html

合并与分割 PDF 文件[编辑 | 编辑源代码]

PyPDF2 使用 PdfWriter 类来编辑与输出 pdf 文件。其基本使用方法如下:

from PyPDF2 import PdfReader, PdfWriter

writer = PdfWriter()              #创建一个 PdfWriter 对象。

with open("meta_edit.pdf", "wb") as f: #把 PdfWriter 的内容存储到文件中。
    writer.write(f)

添加页[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/merging-pdfs.html https://pypdf2.readthedocs.io/en/3.x/user/cropping-and-transforming.html

修改元信息[编辑 | 编辑源代码]

我们还可以给 pdf 文件增加元信息。这里以修改元信息为例:

from PyPDF2 import PdfReader, PdfWriter
writer = PdfWriter()              #创建一个PDF写入对象,作为副本对象。

writer.append("example.pdf")      #将原PDF的所有页面导入副本对象。
                                  #将所需的元数据写入副本对象。
writer.add_metadata(
    {
        "/Author": "Martin",
        "/Producer": "Libre Writer",
    }
)

with open("meta_edit.pdf", "wb") as f: #最后,把副本对象的内容存储到文件中。
    writer.write(f)

给 PDF 文件添加水印[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/add-watermark.html

注释与批注[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/reading-pdf-annotations.html https://pypdf2.readthedocs.io/en/3.x/user/adding-pdf-annotations.html

PDF 表单[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/forms.html

压缩 PDF 文件的大小[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/file-size.html

错误[编辑 | 编辑源代码]

https://pypdf2.readthedocs.io/en/3.x/user/suppress-warnings.html