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该文件是泰国字体自行网上 ...
随机推荐
- IOS PickerView使用
- (void)viewDidLoad { [super viewDidLoad]; // 1.创建pickerview // pickerview有默认的frame UIPickerView *pi ...
- OO2019第四单元作业总结
一.本单元两次作业的架构设计 1.第一次作业 第一次作业由于时间仓促,没有过多的架构设计,就直接补全了所给的MyUmlInteraction类,导致整个程序的代码风格和效率都不高,在强测中也因此失掉 ...
- Ubuntu安装mysql和简单使用
一.安装mysql sudo apt-get install mysql-server sudo apt-get isntall mysql-client sudo apt-get install l ...
- PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)
http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...
- java Html&JavaScript面试题:用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
- oc字符串截取 数组字典运用
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS_ ...
- 已解决: idea创建并部署SpringMVC项目时 报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
用IDEA创建并运行SpringMVC项目时,最初发现没有Servlet包,这个问题已在上篇解决,然而当我们尝试去运行此时的SpringMVC项目时,发现仍然有错误.ClassNotFoundExce ...
- webSocket使用心跳包实现断线重连
首先new一个webscoket的连接 let noticeSocketLink = new WebSocket(‘webSocket的地址’) 这里是连接成功之后的操作 linkNoticeWebs ...
- 1412: [ZJOI2009]狼和羊的故事
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4017 Solved: 2037[Submit][Status][Discuss] Descript ...