跳至內容

使用者: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