python作爲一種具有相對簡單語法的高級解釋語言,即使對於那些沒有編程經驗的人來說,Python也是簡單易操作的。強大的Python庫讓你事半功倍。
在處理文本信息時,通常我們需要從word、PDF文檔中提取出信息,而PDF是最重要和最廣泛使用的用來呈現和交換文件的數字媒體之一,。PDF包含有用的信息,鏈接和按鈕,表單域,音頻,視頻和業務邏輯。python庫很好地集成並提供處理非結構化數據源。運用python可以輕鬆從PDF中提取有用信息後,您可以輕鬆地將該數據用於任何機器學習或自然語言處理模型。
常見的Python庫
以下是可用於處理PDF文件的一些Python庫
PDFMiner :一個從PDF文檔中提取信息的工具。與其他PDF相關工具不同,它完全專注於獲取和分析文本數據。
PyPDF2 :一個純python PDF庫,能夠分割,合併,裁剪和轉換PDF文件的頁面。它還可以向PDF文件添加自定義數據,查看選項和密碼。它可以從PDF中檢索文本和元數據,以及將整個文件合併在一起。
Tabula-py:一個 tabula-java的簡單Python包裝器,它可以讀取PDF表。您可以從PDF讀取表格並轉換爲pandas的DataFrame。tabula-py還允許您將PDF文件轉換爲CSV / TSV / JSON文件。
Slate:PDFMiner的包裝器實現
PDFQuery:pdfminer,lxml和pyquery的輕量級包裝器。它旨在使用儘可能少的代碼可靠地從PDF集合中提取數據。
xpdf :xpdf的 Python包裝器(目前只是“pdftotext”實用程序)
從pdf中提取文本
使用PyPDF2從pdf中提取簡單文本,示例代碼如下:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">import PyPDF2
pdf file object
you can find find the pdf file with complete code in below
pdfFileObj = open('example.pdf', 'rb')
pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
number of pages in pdf
print(pdfReader.numPages)
a page object
pageObj = pdfReader.getPage(0)
extracting text from page.
this will print the text you can also save that into String
print(pageObj.extractText())
</pre>
從pdf中讀取表格數據
使用Pdf中的Table數據,我們可以使用Tabula-py,示例代碼如下:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">import tabula
readinf the PDF file that contain Table Data
you can find find the pdf file with complete code in below
read_pdf will save the pdf table into Pandas Dataframe
df = tabula.read_pdf("offense.pdf")
in order to print first 5 lines of Table
df.head()
</pre>
如果您的Pdf文件包含多個表,可以進行如下設置:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">df = tabula.read_pdf(“crime.pdf”,multiple_tables = True)
</pre>
還可以從任何特定PDF頁面的特定部分提取信息
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">tabula.read_pdf(“crime.pdf”,area =(126,149,212,462),pages = 1)
</pre>
設置讀取輸出爲JSON格式
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">tabula.read_pdf(“crime.pdf”,output_format =“json”)
</pre>
將Pdf導出到Excel
使用以下代碼將PDF數據轉換爲Excel或CSV
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">tabula.convert_into(“crime.pdf”,“crime_testing.xlsx”,output_format =“xlsx”)
</pre>
好啦分享到這裏,如果你跟我一樣都喜歡python,想成爲一名優秀的程序員,也在學習python的道路上奔跑,歡迎你加入python學習羣:839383765 羣內每天都會分享最新業內資料,分享python免費課程,共同交流學習,讓學習變(編)成(程)一種習慣!