python提取pdf文档中的表格数据、svg格式转换为pdf

时间:2022-07-24
本文章向大家介绍python提取pdf文档中的表格数据、svg格式转换为pdf,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

提取pdf文件中的表格数据原文链接

https://www.analyticsvidhya.com/blog/2020/08/how-to-extract-tabular-data-from-pdf-document-using-camelot-in-python/

另外还参考了这篇文章

https://camelot-py.readthedocs.io/en/master/

实现提取pdf文档中的表格数据需要使用camelot模块

这个模块可以直接使用pip进行安装

pip install "camelot-py[cv]"

用到的pdf示例文件可以直接在原文链接处下载

http://gstcouncil.gov.in/sites/default/files/gst-revenue-collection-march2020.pdf

第一步是读入pdf文件

import camelot
tables = camelot.read_pdf('gst-revenue-collection-march2020.pdf', flavor='stream', pages='0-3')

这里flavor参数的作用暂时还不知道

如果表格跨页需要指定pages参数

tables
tables[2]
tables[2].df

tables可以返回解析获得的表格数量

tables[2]获取指定的表格

tables[2].df将表格数据转换成数据框

pandas 中两个数据框按照行合并需要用到append()方法

aa = {"A":[1,2,3],"B":[4,5,6]}
bb = {"A":[4],"B":[7]}
import pandas as pd
a = pd.DataFrame(aa)
b = pd.DataFrame(bb)
a.append(b)

SVG格式转换为pdf格式原文链接

https://www.tutorialexample.com/a-simple-guide-to-python-convert-svg-to-pdf-with-svglib-python-tutorial/

实现这个功能需要使用到的是svglib这个库,直接使用pip安装

pip install svglib

svg转换为pdf格式代码

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
drawing = svg2rlg("home.svg")
renderPDF.drawToFile(drawing, "file.pdf")