camelot工具进行pdf表格解析重建
camelot内置生成html文件的方法,但表格数据转化成pandas.dataframe的过程中,丢失了跨行跨列的结构信息,故生成html的表格无跨行跨列结构。
于是我在输出部分选择直接手写html表格..
import camelot
import numpy as np
import matplotlib.pyplot as plt
import os
import pandas as pd # def listdir(path, list_name): # 传入存储的list
# for file in os.listdir(path):
# file_path = os.path.join(path, file)
# if os.path.isdir(file_path):
# listdir(file_path, list_name)
# else:
# list_name.append(file_path)
#批量文件
# filenames=[r'E:\pdf_download']
# listdir('E:\pdf_download',filenames)
# for onefile in filenames:
# filename=onefile.split(".", )[0] #单个文件
onefile=r'1202007288.pdf'
print("loading...", onefile)
tables = camelot.read_pdf(onefile,pages='',strip_text=' .\n',line_scale=80,split_text=True) for onetable in tables:
mask = np.zeros((len(onetable.rows)+1, len(onetable.cols)+1))
colspan = np.ones((len(onetable.rows)+1, len(onetable.cols)+1))
rowspan = np.ones((len(onetable.rows)+1, len(onetable.cols)+1))
for onerow in onetable.cells:
for onecell in onerow:
thisrow = onetable.cells.index(onerow)
thiscol = onerow.index(onecell)
if mask[thisrow][thiscol] == 0:
if not onecell.right:
for i in range(thiscol,len(onerow)-1):
if not onerow[i].right:
mask[thisrow][i + 1] = 1
colspan[thisrow][thiscol] += 1
else:
break
if not onecell.bottom:
for i in range(thisrow,len(onetable.cells)-1):
if not onetable.cells[i][thiscol].bottom:
mask[i + 1][thiscol] = 1
rowspan[thisrow][thiscol] += 1
else:
break
head='''<table border="1" class="dataframe">
<tbody>'''
f = open(onefile + '-page'+str(onetable.page) + '-table-'+str(onetable.order)+'.html', 'w')
f.write(head)
for onerow in onetable.cells:
writerow = '''
<tr>'''
f.write(writerow)
for onecell in onerow:
thisrow = onetable.cells.index(onerow)
thiscol = onerow.index(onecell)
if mask[thisrow][thiscol] == 0:
if int(colspan[thisrow][thiscol]) > 1:
Colspan = 'colspan=' + str(int(colspan[thisrow][thiscol]))
else:
Colspan=''
if int(rowspan[thisrow][thiscol]) > 1:
Rowspan = 'rowspan=' + str(int(rowspan[thisrow][thiscol]))
else:
Rowspan = ''
writecell = '''
<td %s %s>%s</td>'''%(Colspan,Rowspan,onecell.text)
f.write(writecell)
writerow = '''
</tr>'''
f.write(writerow)
f.close()
camelot工具进行pdf表格解析重建的更多相关文章
- java(itext) 一个很简单的PDF表格生成工具
先上个效果图 因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类. 如果需要编写样式或者插入背景,都可以查阅itex官 ...
- Java iText5.5.1 绘制PDF表格
iText下载链接:http://sourceforge.net/projects/itext/files/ 会有两个文件夹:extrajars中的extrajars-2.3.jar文件用于解决中文不 ...
- Java添加条形码到PDF表格
条码的应用已深入生活和工作的方方面面.在处理条码时,常需要和各种文档格式相结合.当需要在文档中插入.编辑或者删除条码时,可借助于一些专业的类库工具来实现.本文,以操作PDF文件为例,介绍如何在编辑表格 ...
- ITextSharp导出PDF表格和图片(C#)
文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...
- itextSharp 附pdf文件解析
一.PdfObject: pdf对象 ,有9种,对象是按照对象内涵来分的,如果按照对象的使用规则来说,对象又分为间接对象和直接对象.间接对象是PDF中最常用的对象,如前面对象集合里面的,所有对象都是间 ...
- MVC 生成PDf表格并插入图片
最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextShar ...
- Python使用Tabula提取PDF表格数据
今天遇到一个批量读取pdf文件中表格数据的需求,样式大体是以下这样: python读取PDF无非就是三种方式(我所了解的),pdfminer.pdf2htmlEX 和 Tabula.综合考虑后,选择了 ...
- spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...
- Java 生成pdf表格文档
最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...
随机推荐
- 在Spark集群中,集群的节点个数、RDD分区个数、cpu内核个数三者与并行度的关系
梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...
- 【BZOJ2049】[SDOI2008] Cave 洞穴勘测(LCT维护连通性)
点此看题面 大致题意: 有\(n\)个洞穴,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 这道题应该是\(LCT\)动态维护连通性的一道模板题. 考虑将\(x\) ...
- springboot框架快速搭建
1. 新建Maven项目 spring-boot 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- 漫谈 Clustering (番外篇): Dimensionality Reduction
由于总是有各种各样的杂事,这个系列的文章竟然一下子拖了好几个月,(实际上其他的日志我也写得比较少),现在决定还是先把这篇降维的日志写完.我甚至都以及忘记了在这个系列中之前有没有讲过“特征”(featu ...
- 纯js实现淘宝商城轮播图
需求: 循环无缝自动轮播3张图片,点击左右箭头可以手动切换图片,鼠标点击轮播图下面的小圆点会跳转到对应的第几张图片.鼠标放到轮播图的图片上时不再自动轮播,鼠标移开之后又继续轮播.效果图: 下面是htm ...
- json文件的读取
在客户端读取后台的json文件,使用jquery的$.getJSON,读取后台文件内容. jQuery中的$.getJSON( )方法函数主要用来从服务器加载json编码的数据,它使用的是GET HT ...
- notify()和notifyAll()主要区别
notify()和notifyAll()都是Object对象用于通知处在等待该对象的线程的方法. void notify(): 唤醒一个正在等待该对象的线程.void notifyAll(): 唤醒所 ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- 3170: [Tjoi2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1804 Solved: 968[Submit][Status][Discuss] Descript ...
- 记录一些 FileZillaClient 的基本连接操作
本地主机:Window 10 FileZilla版本:3.39.0 64位 远程主机:CentOS 6.4 需安装FTP服务 小提示:查看CentOS版本命令 # cat /etc/issue Fil ...