【Pyhthon读取 PDF文件表格 ,转为 CSV/TSV/JSON文件】

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 脚本实现自动化
  • 转换 pandas DataFrame 后的高级分析
  • 使用 Jupyter 笔记本或 Google Colabolatory 进行随意分析

环境和安装

检查 Java 环境并安装 tabula-py

tabula-py 需要 java 环境,因此让我们检查您机器上的 java 环境

复制代码
!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() 函数检查您的环境,该函数显示 Python 版本、Java 版本和您的操作系统环境。

复制代码
import tabula

tabula.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。

bb 复制代码
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,如下所示:

bb 复制代码
dfs = tabula.read_pdf("test.pdf", pages='all')"test.pdf", pages='all')

要避免的几个错误:

  1. 确保你安装了 tabula-py 而不仅仅是 tabula 使用
bb 复制代码
!pip install tabula-py
#and to import it use
from tabula.io import read_pdf
  1. 如果您在安装 tabula-py 之前意外安装了 tabula,它们会在命名空间中发生冲突(即使在卸载 tabula 之后)。卸载 tabula-py 并重新安装。

最后

很简单,对吧?实际上,你可以向 Tabula 传递很多不同的命令来加快速度,甚至可以提供特定的 XY 坐标进行提取。

相关推荐
阿坤带你走近大数据9 分钟前
java中泛型不能用基础数据类型
java·开发语言
weixin_3077791312 分钟前
从脚本执行到智能体协作:AI辅助测试能力的范式重构
运维·开发语言·人工智能·算法·测试用例
量化君也13 分钟前
从回测到全自动实盘交易,全天候策略需要经历哪些改造?
大数据·人工智能·python·算法·金融
装不满的克莱因瓶18 分钟前
自然语言处理发展历史——从规则系统到大语言模型的演进之路
网络·人工智能·python·深度学习·语言模型·自然语言处理
云絮.23 分钟前
增删改查操作
java·开发语言
2601_9516457829 分钟前
Linux 编程语言全解析:C、C++、Python、Go、Rust 谁更强?
linux·python·go·c·编程语言
themingyi30 分钟前
Abaqus2024安装python包pandas
开发语言·python·pandas
阿正的梦工坊35 分钟前
【Rust】19-FFI、ABI 与跨语言边界设计
开发语言·后端·rust
殇淋狱陌38 分钟前
Python列表知识思维导图
开发语言·python·学习
代码中介商40 分钟前
C++ 智能指针完全指南(三):weak_ptr 与循环引用
开发语言·c++