python提取图片中的表格,Python从图像中的表中提取值
我要从PDF表格中提取值,然后将数据保存在json文件中,
我使用pytesseract从将pdf转换为的图像中获取文本,
现在的问题是,它给了我一个包含所有数据的大字符串。
我尝试通过SP进行拆分,这逐行给了我数据,我现在想要的是一种基于列名分离数据并保存为json格式的方法。
以下是具有表的图像。
就是说,我需要一个数组,我可以在其中保存值是SP的所有者,
资产包含该列中的所有数据,
**以下是从图像中获取全文的代码:**
import cv2
import pytesseract as tess
tess.pytesseract.tesseract_cmd = r'C:\Users\faisa\AppData\Local\Tesseract-OCR\tesseract.exe'
from PIL import Image
# img = Image.open("image.png")
img = cv2.imread("Test 1.png")
text = tess.image_to_string(img)
print(text)
结果是:
Filing ID #20018011
PERIODIC TRANSACTION REPORT
Clerk of the House of Representatives » Legislative Resource Center ¢ 135 Cannon Building « Washington, DC 20515
FILer INFORMATION
Name: Hon. Nancy Pelosi
Status: Member
State/District: CA12
TRANSACTIONS
ID Owner Asset Transaction Date Notification Amount Cap.
Type Date Gains >
$200?
SP AllianceBernstein Holding L.P. P 12/22/2020 12/22/2020 $500,001 - ‘I
Units (AB) [ST] $1,000,000
FILinc Status: New
DescriPTION: Purchased 20,000 shares.
SP Apple Inc. (AAPL) [OP] P 12/22/2020 12/22/2020 $250,001 - ~
$500,000
Fiuine Status: New
Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.
SP Tesla, Inc. (TSLA) [OP] P 12/22/2020 12/22/2020 $500,001 - ~
$1,000,000
Fiine Status: New
Description: Purchased 25 call options with a strike price of $500 and an expiration date of 3/18/22.
SP Walt Disney Company (DIS) [OP] P 12/22/2020 12/22/2020 $500,001 - ‘a
$1,000,000
Fiuine Status: New
Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.
* For the complete list of asset type abbreviations, please visit https: //fd.house.gov/reference/asset-type-codes.aspx.
INITIAL PuBLic OFFERINGS
© Yes © No
CERTIFICATION AND SIGNATURE
lv 1 CERTIFY that the statements I have made on the attached Periodic Transaction Report are true, complete, and correct to the
这是我将表中的行拆分为不同行并保存到临时字典的部分。
result = """Filing ID #20018011
PERIODIC TRANSACTION REPORT
Clerk of the House of Representatives » Legislative Resource Center ¢ 135 Cannon Building « Washington, DC 20515
FILer INFORMATION
Name: Hon. Nancy Pelosi
Status: Member
State/District: CA12
TRANSACTIONS
ID Owner Asset Transaction Date Notification Amount Cap.
Type Date Gains >
$200?
SP AllianceBernstein Holding L.P. P 12/22/2020 12/22/2020 $500,001 - ‘I
Units (AB) [ST] $1,000,000
FILinc Status: New
DescriPTION: Purchased 20,000 shares.
SP Apple Inc. (AAPL) [OP] P 12/22/2020 12/22/2020 $250,001 - ~
$500,000
Fiuine Status: New
Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.
SP Tesla, Inc. (TSLA) [OP] P 12/22/2020 12/22/2020 $500,001 - ~
$1,000,000
Fiine Status: New
Description: Purchased 25 call options with a strike price of $500 and an expiration date of 3/18/22.
SP Walt Disney Company (DIS) [OP] P 12/22/2020 12/22/2020 $500,001 - ‘a
$1,000,000
Fiuine Status: New
Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.
* For the complete list of asset type abbreviations, please visit https: //fd.house.gov/reference/asset-type-codes.aspx.
INITIAL PuBLic OFFERINGS
© Yes © No
CERTIFICATION AND SIGNATURE
lv 1 CERTIFY that the statements I have made on the attached Periodic Transaction Report are true, complete, and correct to the
ye to"""
table = []
row = 1
for temp_ar in result.strip().split('\nSP ')[1:]:
temp_ar = temp_ar.replace('~','').replace("‘a",'').replace("‘I",'')
line1 = temp_ar.split(',000\n')[0] + ",000"
line2 = temp_ar[temp_ar.index(line1)+len(line1):].split('\nDescription:')[0]
line3 = temp_ar[temp_ar.index(line2)+len(line2):].split("For the complete list")[0]
table.append({
"row" : row,
'row_{}_line_1'.format(row): line1.strip().replace('\n',''),
'row_{}_line_2'.format(row): line2.strip().replace('\n',''),
'row_{}_line_3'.format(row): line3.strip().replace('\n',''),
})
row += 1
print(table)
结果是:
[{'row': 1, 'row_1_line_1': 'AllianceBernstein Holding L.P. P 12/22/2020 12/22/2020 $500,001 - Units (AB) [ST] $1,000,000', 'row_1_line_2': 'FILinc Status: NewDescriPTION: Purchased 20,000 shares.', 'row_1_line_3': ''}, {'row': 2, 'row_2_line_1': 'Apple Inc. (AAPL) [OP] P 12/22/2020 12/22/2020 $250,001 - $500,000', 'row_2_line_2': 'Fiuine Status: New', 'row_2_line_3': 'Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.'}, {'row': 3, 'row_3_line_1': 'Tesla, Inc. (TSLA) [OP] P 12/22/2020 12/22/2020 $500,001 - $1,000,000', 'row_3_line_2': 'Fiine Status: New', 'row_3_line_3': 'Description: Purchased 25 call options with a strike price of $500 and an expiration date of 3/18/22.'}, {'row': 4, 'row_4_line_1': 'Walt Disney Company (DIS) [OP] P 12/22/2020 12/22/2020 $500,001 - $1,000,000', 'row_4_line_2': 'Fiuine Status: New', 'row_4_line_3': 'Description: Purchased 100 call options with a strike price of $100 and an expiration date of 1/21/22.*'}]
现在您可以看到它混合了1行中的列,并且很难动态拆分每个列并合并其行。
我确实尝试通过检测是否是日期来分割第一行的每一行,但是不走运,它没有检测到日期。
我需要这个脚本来处理类似的PDF文件,因此我需要一种动态的方式来找出该列,任何帮助将不胜感激。谢谢。
解决方案
暂无回答
python提取图片中的表格,Python从图像中的表中提取值相关推荐
- python对图片中的表格拆分
python对图片中的表格拆分 背景:为了对图片中的表格进行ocr识别,如果直接识别效果较差,所以将表格拆分成小块一个个识别,然后再拼成表格即可. 拆分规则的表格: import cv2 import ...
- 把Excel表格通过MySql Workbench导入数据库表中的使用总结
把Excel表格通过MySql Workbench导入数据库表中的使用总结 今天接到一个任务,把excel表中的数据导入到mysql数据库中,通过半个多小时的鼓捣,基本上摸清了里面的门道. 首先,准备 ...
- 人口普查分析:利用python+百度文字识别提取图片中的表格数据
今天发布了最新的人口普查结果,笔者拿到的文件是pdf格式(网上应该有).之前就一直想实现从pdf提取表格数据,输出为excel.正好这次有公开数据,因此打算用来练个手. 尝试了两种方法: 1.pyth ...
- python pdfminer读取pdf表格_如何使用PDFMiner从pdf中提取表格?
我正试图从pdf文档中的一些表中提取信息. 考虑输入:Title 1 some text some text some text some text some text some text some ...
- python如何在exel中编程_如何使用Python以编程方式将行添加到现有Excel表中
尽管有各种各样的pythonexcel操作库和资源,但我无法找到具体的解决方案.在 现在,我有一个表格存在的Excel模板文件.我想编写一个Python程序来填充这个表.对于任何现有的Excel库都可 ...
- 【python】将多个tsv文件合并到excel表中
需求 将某个目录下多个特定文本文件,合并到excel表中.并要求将文件名作为sheet名,每个sheet中第一行第一个单元格填写对应文件的所在路径,单元格格式使用Times New Roman. 使用 ...
- access 套用表格_在Excel 2013工作表中导入Access数据的方法
Excel与Access同为数据处理软件,它们在功能上各有侧重.Access精于数据管理和分类,而Excel则长于数据统计和计算.这两个软件处理过的数据是可以互相调用的,综合应用这两个软件,取长补短, ...
- python爬虫代码提取图片中的文字_python识别图像并提取文字的实现方法
前言 python图像识别一般基础到的就是tesseract了,在爬虫中处理验证码广泛使用. 安装 安装教程网上大都差不多,Windows下确实比较麻烦,涉及到各种路径.环境变量甚至与linux不同的 ...
- python 截取图片中的一部分并且复制到另一张图片中 (任意角度旋转)
目的:截取左面均压环,并且对截取的区域进行旋转和复制 进行数据增强 import PIL.Image as img import osIMG = 'C:\\Users\\Ilearn\\Desktop ...
最新文章
- mysql低权限用户getshell_GetShell的姿势总结
- transition过渡规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。...
- verilog中如何拆分一个数
- mysql 查看当前连接数 和 最大连接数
- Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务
- 超低延迟直播架构解析
- Entity Framework 6以Code First方式搭建Sqlite数据库环境
- 初识BGP外部网关协议(一)
- 如何使用Java 8 FlatMap JDBC ResultSet?
- 【普通の随笔】6.30
- HBase 管理,性能调优
- TensorFlow 学习(四)—— computation graph
- 金融科技领域的安全威胁及金融科技安全分析
- delphi 剪切板变量_delphi实时监控剪贴板
- spwm matlab仿真,H桥逆变器SPWMMATLAB仿真
- 华为eNSP下载与安装
- 阿里巴巴Java开发手册 终极版
- 论文阅读:SCAFFOLD: Stochastic Controlled Averaging for Federated Learning
- 在命令行cmd 下,输入dxdiag 查看关于电脑许多硬件的详细信息
- 笔记本Ubuntu系统,关上盖子不休眠设置
热门文章
- MacBook解压安装MongoDB
- java使用在线编辑器生成PDF
- 这一次,Windows 站起来了:Windows ​ Linux 的性能 Battle!
- 【腾讯出品】运维自动化新玩法——标准运维企业应用案例分享
- 输入一个正整数n,再输入n个学生的姓名和百分制成绩,将其转换为两级制成绩后输出。要求定义和调用函数set_grade(stu, n),其功能是根据结构数组stu中存放的学生的百分制成绩score,将其
- Hive必须了解的技能有哪些?万字博客带你掌握Hive
- thymeleaf利用th:each循环Map中的List(Map嵌套循环)
- win10“此电脑”下的几个文件夹怎删除?
- 画一个清晰明了的时序图,要掌握这三点
- php 日期1900年开始,两个日期类-PHP教程,PHP应用