python tabula 使用方法,如何使用tabula-py将PDF转换为CSV?

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

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

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

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

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

解决方案

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

aa3b067ef03d92cb723360a02b08081a.png

You can save the DataFrame as csv afterwards:

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值