東營建設信息網站百度手機導航官方新版
tabula-py
tabula-py 是一個將 PDF 表格轉換為 pandas DataFrame 的工具。
tabula-py 是 tabula-java 的包裝器,需要您的機器上有 java。
tabula-py 還允許您將 PDF 中的表格轉換為 CSV/TSV 文件。
tabula-py 的 PDF 提取準確度與 tabula-java 或 tabula app 相同;tabula 的 GUI 工具,因此如果您想知道 tabula-py 的性能,我強烈建議您嘗試 tabula app。
tabula-py 適用于:
- 使用 Python 腳本實現(xiàn)自動化
- 轉換 pandas DataFrame 后的高級分析
- 使用 Jupyter 筆記本或 Google Colabolatory 進行隨意分析
環(huán)境和安裝
檢查 Java 環(huán)境并安裝 tabula-py
tabula-py 需要 java 環(huán)境,因此讓我們檢查您機器上的 java 環(huán)境
!java -version
openjdk version "11.0.20" 2023-07-18 OpenJDK Runtime Environment (build 11.0.20+8-post-Ubuntu-1ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.20+8-post-Ubuntu-1ubuntu120.04, mixed mode, sharing)安裝bula-py by using pip.
!pip install -q tabula-py
在嘗試 tabula-py 之前,請通過 tabula-py environment_info() 函數(shù)檢查您的環(huán)境,該函數(shù)顯示 Python 版本、Java 版本和您的操作系統(tǒng)環(huán)境。
import tabulatabula.environment_info()
Python version:3.8.10 (default, May 26 2023, 14:05:08) [GCC 9.4.0] Java version:openjdk version "11.0.20" 2023-07-18 OpenJDK Runtime Environment (build 11.0.20+8-post-Ubuntu-1ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.20+8-post-Ubuntu-1ubuntu120.04, mixed mode, sharing) tabula-py version: 2.7.1.dev6+gd9154b3 platform: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.29 uname:uname_result(system='Linux', node='GARM', release='5.15.90.1-microsoft-standard-WSL2', version='#1 SMP Fri Jan 27 02:56:13 UTC 2023', machine='x86_64', processor='x86_64') linux_distribution: ('Ubuntu', '20.04', 'focal') mac_ver: ('', ('', '', ''), '')
使用
GitHub 倉庫鏈接:LinkedIn
示例代碼
ttabula-py 可讓您將 PDF 中的表格提取到 DataFrame 或 JSON 中。它還可以從 PDF 中提取表格并將文件保存為 CSV、TSV 或 JSON。
import tabula# Read pdf into list of DataFrame
dfs = tabula.read_pdf("test.pdf", pages='all')# Read remote pdf into list of DataFrame
dfs2 = tabula.read_pdf("https://github.com/tabulapdf/tabula-java/raw/master/src/test/resources/technology/tabula/arabic.pdf")# convert PDF into CSV file
tabula.convert_into("test.pdf", "output.csv", output_format="csv", pages='all')# convert all PDFs in a directory
tabula.convert_into_by_batch("input_directory", output_format='csv', pages='all')
如果我們想讀取 pdf 的所有頁面怎么辦?好吧,你需要做的就是將標志傳遞pages=’all’
?給 Tabula,如下所示:
dfs = tabula.read_pdf("test.pdf", pages='all')"test.pdf", pages='all')
要避免的幾個錯誤:
- 確保你安裝了 tabula-py 而不僅僅是 tabula 使用
!pip install tabula-py
#and to import it use
from tabula.io import read_pdf
2. 如果您在安裝 tabula-py 之前意外安裝了 tabula,它們會在命名空間中發(fā)生沖突(即使在卸載 tabula 之后)。卸載 tabula-py 并重新安裝。
最后
很簡單,對吧?實際上,你可以向 Tabula 傳遞很多不同的命令來加快速度,甚至可以提供特定的 XY 坐標進行提取。
?