Python 如何使用 tabula-py 将 PDF 转换为 CSV?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49560486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:08:03  来源:igfitidea点击:

How to convert PDF to CSV with tabula-py?

pythoncsvpdftabula

提问by Reinaldo Chaves

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

在 Python 3 中,我有一个包含 6,041 页的 PDF 文件“Ativos_Fevereiro_2018_servidores_rj.pdf”。我在一台装有 Ubuntu 的机器上

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

在每一页的顶部都有两行文本。在表格下方,带有标题和两列。每个表 36 行,最后一页少

At the end of each page, after the tables, there is also a line of text

在每一页的末尾,在表格之后,还有一行文字

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

我想从这个 PDF 创建一个 CSV,只考虑页面中的表格。忽略表格前后的文字

Initially I tested the tabula-py. But it generates an empty file:

最初我测试了 tabula-py。但它会生成一个空文件:

from tabula import convert_into

convert_into("Ativos_Fevereiro_2018_servidores_rj.pdf", "test_s.csv", output_format="csv")

Please, does anyone know of another method to use tabula-py for this type of demand?

请问,有没有人知道另一种使用 tabula-py 来满足这种需求的方法?

Or another way to convert PDF to CSV in this file type?

或者另一种将这种文件类型的 PDF 转换为 CSV 的方法?

采纳答案by ilja

Ok, I've found the issue: you have to set spreadsheet=Trueand keep utf-8 encoding:

好的,我发现了问题:您必须设置spreadsheet=True并保持 utf-8 编码:

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):

在下图中,我仅使用第一页对其进行了测试(因为您的文件很大):

enter image description here

在此处输入图片说明

You can save the DataFrame as csv afterwards:

之后您可以将 DataFrame 保存为 csv:

df.to_csv('otuput.csv', encoding='utf-8')

Edit:

编辑:

Ok, the error could be a java-memory issue. To make it faster I added the pagesoption. 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.

好的,错误可能是 java 内存问题。为了使它更快,我添加了该pages选项。而且还有一个编码问题,所以encoding='utf-8'添加到csv导出中。如果您一直遇到 java 错误,请尝试将其分块解析,例如pages='1-300'. 我刚刚完成了所有 6041(在 64GB RAM 机器上),它运行良好。