从PDF文件中提取表格数据,你可以使用多种Python库,其中最流行的有`tabula-py`和`pdfplumber`。以下是使用这些库提取表格的步骤:
使用tabula-py提取表格
安装tabula-py
```bash
pip install tabula-py
```
导入库
```python
import tabula
```
读取PDF文件中的表格
```python
tables = tabula.read_pdf("example.pdf", pages="all")
```
打印提取到的表格数量
```python
print(f"共提取到 {len(tables)} 个表格")
```
查看第一个表格的内容
```python
print(tables)
```
保存提取的表格
```python
for i, table in enumerate(tables):
table.to_csv(f"table_{i+1}.csv", index=False)
```
使用pdfplumber提取表格
安装pdfplumber
```bash
pip install pdfplumber
```
导入库
```python
import pdfplumber
import pandas as pd
```
提取PDF中的所有表格
```python
with pdfplumber.open(pdf_path) as pdf:
tables = []
for page in pdf.pages:
page_tables = page.extract_table()
if page_tables:
tables.extend(page_tables)
```
将提取的表格转换为DataFrame并保存为CSV
```python
df_list = [pd.DataFrame(table) for table in tables]
df_list.to_csv("output.csv", index=False)
```
使用Camelot提取表格
安装Camelot
```bash
pip install camelot-py[cv]
```
导入库
```python
import camelot
```
读取PDF文件中的表格
```python
tables = camelot.read_pdf('foo.pdf')
```
将提取的表格转换为DataFrame并保存为CSV
```python
tables.to_csv('foo.csv', f='csv', compress=True)
```
这些方法可以帮助你从PDF文件中提取表格数据,并将其保存为CSV文件。你可以根据自己的需求选择合适的库和方法。