如何使用tabula-py将PDF转换为CSV? [英] How to convert PDF to CSV with tabula-py?
问题描述
在Python 3中,我有一个PDF文件"Ativos_Fevereiro_2018_servidores_rj.pdf",具有6,041页.我在装有Ubuntu的计算机上
In Python 3, I have a PDF file "Ativos_Fevereiro_2018_servidores_rj.pdf" with 6,041 pages. I'm on a machine with Ubuntu
在每个页面的顶部,两行都是文本.在表格下方,带有标题和两列.每张表排成36行,最后一页则减少
On each page there is text at the top of the page, two lines. And below a table, with header and two columns. Each table in 36 rows, less on the last page
在每页末尾,表格之后,还有一行文字
At the end of each page, after the tables, there is also a line of text
我想从此PDF创建CSV,仅考虑页面中的表格.并忽略表格前后的文字
I want to create a CSV from this PDF, considering only the tables in the pages. And ignoring the texts before and after the tables
最初,我测试了表格.但它会生成一个空文件:
Initially I tested the tabula-py. But it generates an empty file:
from tabula import convert_into
convert_into("Ativos_Fevereiro_2018_servidores_rj.pdf", "test_s.csv", output_format="csv")
请问,有人知道这种方法可以使用tabula-py吗?
Please, does anyone know of another method to use tabula-py for this type of demand?
还是将这种文件类型的PDF转换为CSV的另一种方法?
Or another way to convert PDF to CSV in this file type?
推荐答案
好,我发现了问题:您必须设置spreadsheet=True
并保持utf-8编码:
Ok, I've found the issue: you have to set spreadsheet=True
and keep utf-8 encoding:
df = tabula.read_pdf("Ativos_Fevereiro_2018_servidores_rj.pdf", encoding='utf-8', spreadsheet=True, pages='1-6041')
在下面的图片中,我仅在首页进行了测试(因为您的文件很大):
In the picture below I tested it with just the first page (because your file is huge):
之后,您可以将DataFrame另存为csv:
You can save the DataFrame as csv afterwards:
df.to_csv('otuput.csv', encoding='utf-8')
好的,该错误可能是Java内存问题.为了使其更快,我添加了pages
选项.此外,还有一个编码问题,因此encoding='utf-8'
已添加到csv导出中.
如果您仍然遇到Java错误,请尝试分批解析它,例如pages='1-300'
.我刚刚完成了所有6041(在64GB RAM机器上),工作正常.
Ok, the error could be a java-memory issue. To make it faster I added the pages
option. And there also was an encoding problem, so encoding='utf-8'
added to the csv export.
If you keep running into the java-error, try parse it in chunks, e.g. pages='1-300'
. I just did all 6041 (on a 64GB RAM Machine), it worked fine.
这篇关于如何使用tabula-py将PDF转换为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!