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表格解析重建的更多相关文章

  1. java(itext) 一个很简单的PDF表格生成工具

    先上个效果图 因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类. 如果需要编写样式或者插入背景,都可以查阅itex官 ...

  2. Java iText5.5.1 绘制PDF表格

    iText下载链接:http://sourceforge.net/projects/itext/files/ 会有两个文件夹:extrajars中的extrajars-2.3.jar文件用于解决中文不 ...

  3. Java添加条形码到PDF表格

    条码的应用已深入生活和工作的方方面面.在处理条码时,常需要和各种文档格式相结合.当需要在文档中插入.编辑或者删除条码时,可借助于一些专业的类库工具来实现.本文,以操作PDF文件为例,介绍如何在编辑表格 ...

  4. ITextSharp导出PDF表格和图片(C#)

    文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...

  5. itextSharp 附pdf文件解析

    一.PdfObject: pdf对象 ,有9种,对象是按照对象内涵来分的,如果按照对象的使用规则来说,对象又分为间接对象和直接对象.间接对象是PDF中最常用的对象,如前面对象集合里面的,所有对象都是间 ...

  6. MVC 生成PDf表格并插入图片

    最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextShar ...

  7. Python使用Tabula提取PDF表格数据

    今天遇到一个批量读取pdf文件中表格数据的需求,样式大体是以下这样: python读取PDF无非就是三种方式(我所了解的),pdfminer.pdf2htmlEX 和 Tabula.综合考虑后,选择了 ...

  8. spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)

    一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...

  9. Java 生成pdf表格文档

    最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...

随机推荐

  1. C#中split的方法汇总

    字符串的处理往往离不开split方法,下面介绍几种split的用法: 1.对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) string s=abcdeabcdeabcde; st ...

  2. 爬虫技术-httpClent+jsoup

    技术:httpClent+jsoup 任务:利用httpClent爬去网站信息,在利用jsoup解析 方法说明: parseUrl(String url):传入相应的url返回该网页内容,网页必须是h ...

  3. ASP.NET MVC 长连接(服务器推)完整实现

    1.什么是"服务器推"(百科来一波)? 传统模式的 Web 系统以客户端发出请求.服务器端响应的方式工作.这种方式并不能满足很多现实应用的需求,譬如: 监控系统:后台硬件热插拔.L ...

  4. shiro无法进入授权的方法org.crazycake.shiro.exception.PrincipalInstanceException: class java.util.HashMap must has getter for field: id

    rg.crazycake.shiro.exception.PrincipalInstanceException: class java.util.HashMap must has getter for ...

  5. #linux 下Sublime的安装

    1.Download http://www.sublimetext.com/2 Installtion use tar  解压压缩包,这里我将包改了个名字,这样就不用写空格的转义字符了,改成Subli ...

  6. IOS 移除栈顶的控制器

    - (IBAction)back2Two:(id)sender { // 移除当前栈顶的控制器 [self.navigationController popViewControllerAnimated ...

  7. 启动tomcat的Cannot find ./catalina.sh 的问题

    从终端进入tomcat的bin目录,然后执行startup.sh Cannot find bin/catalina.sh The file is absent or does not have exe ...

  8. javaweb基础(30)_EL函数库

    一.EL函数库介绍 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 这些EL函数在JSTL开发包中进行描述,因此在JSP页 ...

  9. ASP.NET 与 Ajax 的实现方式

    Ajax 应该不是一项技术,是一种思想而已,跟 ASP.NET 以及其它 Web 开发语言没有什么太大关系,这里只是谈谈 ASP.NET 中目前使用的 Ajax 技术以及其它一些实现 Ajax 的优秀 ...

  10. RabbitMQ-消费者"未处理完的消息"丢失

    一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...