在 Python 3 中,我有一个包含 6,041 页的 PDF 文件“Ativos_Fevereiro_2018_servidores.pdf”。我在一台装有 Ubuntu 的机器上。文件在这里:https ://drive.google.com/file/d/1P8kF0gUOVls6sOGed4R0C2PlVF5RFtU6/view?usp=sharing
在每一页的顶部都有文本,两行。在一个表格下方,带有标题和两列。每张表36行,最后一页少
在每一页的最后,表格之后,还有一行文字
我想从这个 PDF 创建一个 CSV,只考虑页面中的表格。并忽略表格前后的文字
为了避免 java-memory 错误,我想我会将文件分成 300 页的组。我在 tabula-py 中这样做了:
import tabula
import pandas as pd
dfs = []
for i in range(1,6041, 300):
if i != 1:
i = i + 1
i2 = i + 300
if i2 > 6041:
i2 = 6041
print(i)
print(i2)
try:
df = tabula.read_pdf("Ativos_Fevereiro_2018.pdf", encoding='latin-1', spreadsheet=True, pages='i-i2', header=0)
dfs.append(df)
print('Page ', len(df), ' parsed.')
except:
print('Error on page: ', i)
output = pd.concat(dfs)
output.to_csv('servidores_rj_ativos_fev_18.csv', encoding='utf-8', index=False)
但我所做的范围是错误的:
1
301
Error: Syntax error in page range specification
Error on page: 1
302
602
...
Error: Syntax error in page range specification
Error on page: 5702
6002
6041
Error: Syntax error in page range specification
Error on page: 6002
Traceback (most recent call last):
File "roboseguranca_pdftocsv.py", line 26, in <module>
output = pd.concat(dfs)
File "/home/reinaldo/Documentos/Code/intercept/seguranca/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 212, in concat
copy=copy)
File "/home/reinaldo/Documentos/Code/intercept/seguranca/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 245, in __init__
raise ValueError('No objects to concatenate')
ValueError: No objects to concatenate
请问,如何纠正范围错误?