python读取json数据还原表格批量转换成html
背景:
由于需要对ocr
识别系统的表格识别结果做验证,通过返回的json
文件结果对比比较麻烦,故需要将json
文件里面的识别结果还原为表格做验证。
文件部分内容如下:
{"row":"6","col","5""start_row": 0, "start_column": 0, "end_row": 0, "end_column": 0, "data": "称", "position": [51, 71, 168, 93], "org_position": [50, 60, 167, 62, 166, 84, 49, 82], "char_position": [[86, 83, 100, 100]], "lines": [{"text": "称", "poly": [84, 73, 98, 73, 98, 90, 84, 90, 0.874], "score": 0.874, "char_centers": [[91, 82]], "char_polygons": [[84, 77, 98, 74, 98, 87, 84, 90]], "char_candidates": [["称"]], "char_candidates_score": [[0.999]], "char_scores": [0.999]}]}
现在需要通过行列的起始和结束坐标以及内容生成相应的表格
开始准备使用js但由于一些语法忘记,所以还是选用python进行。
在经过一些列研究后发现利用python-docx
可自动生成表格,但是格式是word的,所有后期又进行了word转html操作。
一、实操
1.首先创建一个新的文档
然后用Document
类的add_table
方法增加一个表格,其中rows是行,cols是列,style表格样式,具体可以查看官方文档:
上述代码就在word里插入了一个37行、13列的表格。(有37*13=481个cell)
生成的每个cell都是有“坐标”的,比如上面的表格左上角cell为(0,0),右下角cell为(36,12)
下面要做的就是合并一些cell,从而达到我们最终需要的表格
上述代码就将cell(0,0)到cell(2,2)之间的所有cell合并成一个cell
这里需要注意的是,虽然每个cell都合并了,但其实它还是存在的。比如合并了(0,0)和(0,1)两个cell,那么这个合并的cell其实就是(0,0;0,1)
如果cell较多,无法直观的看出坐标的话,可以用下列的代码将每个cell的坐标都标注出来,方便合并
1 2 3 4 5 6 7 8 9 10 11 12 | document = Document() table = document.add_table(rows = 37 ,cols = 13 ,style = 'Table Grid' ) document.save( 'table-1.docx' ) document1 = Document( 'table-1.docx' ) table = document1.tables[ 0 ] for row,obj_row in enumerate (table.rows): for col,cell in enumerate (obj_row.cells): cell.text = cell.text + "%d,%d " % (row,col) document1.save( 'table-2.docx' ) |
2.添加文本
将所有cell依次合并后,就需要向合并后的cell
里添加文本。
用table的row方法可以得到一个表格的一行list其中包含了这一行的所有cell
上面代码就得到了合并表格后的第一行所有cell,然后我们用hdr_cell0[0]就可以得到合并表格后的第一行的第一个cell。用add_paragraph
方法即可像cell里添加文本
其他使用方法可参考官网模块:https://www.osgeo.cn/python-docx/
二、word转成html
1.使用pydocx转换
1 2 3 4 5 | from pydocx import PyDocX html = PyDocX.to_html( "test.docx" ) f = open ( "test.html" , 'w' , encoding = "utf-8" ) f.write(html) f.close() |
通过网页上传word文档,只接收docx
1 2 3 | <form method = "post" enctype = "multipart/form-data" > < input type = "file" name = "file" accept = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" > < / form> |
2.使用win32模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | pip3 install pypiwin32 from win32com import client as wc import os word = wc.Dispatch( 'Word.Application' ) def wordsToHtml( dir ): for path, subdirs, files in os.walk( dir ): for wordFile in files: wordFullName = os.path.join(path, wordFile) doc = word.Documents. Open (wordFullName) wordFile2 = wordFile dotIndex = wordFile2.rfind( "." ) if (dotIndex = = - 1 ): print (wordFullName + "********************ERROR: 未取得后缀名!" ) fileSuffix = wordFile2[(dotIndex + 1 ):] if (fileSuffix = = "doc" or fileSuffix = = "docx" ): fileName = wordFile2[: dotIndex] htmlName = fileName + ".html" htmlFullName = os.path.join(path, htmlName) print ( "generate html:" + htmlFullName) doc.SaveAs(htmlFullName, 10 ) doc.Close() word.Quit() print ("") print ( "Finished!" ) if __name__ = = '__main__' : import sys if len (sys.argv) ! = 2 : print ( "Usage: python funcName.py rootdir" ) sys.exit( 100 ) wordsToHtml(sys.argv[ 1 ]) |
到此这篇关于python读取json数据还原表格批量转换成html的文章就介绍到这了,更多相关python读取json数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
最新评论