广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python读取json数据还原表格批量转换成html
  • 316
分享到

python读取json数据还原表格批量转换成html

2024-04-02 19:04:59 316人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、实操1.首先创建一个新的文档2.添加文本二、Word转成html1.使用pydocx转换2.使用win32模块背景: 由于需要对ocr识别系统的表格识别结果做验证,通过返回的

背景:

由于需要对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操作。

一、实操

pip install python_docx

1.首先创建一个新的文档

from docx import Document
document = Document()

然后用Document类的add_table方法增加一个表格,其中rows是行,cols是列,style表格样式,具体可以查看官方文档:

table = document.add_table(rows=37,cols=13,style='Table Grid')

上述代码就在word里插入了一个37行、13列的表格。(有37*13=481个cell)

生成的每个cell都是有“坐标”的,比如上面的表格左上角cell为(0,0),右下角cell为(36,12)

下面要做的就是合并一些cell,从而达到我们最终需要的表格

table.cell(0,0).merge(table.cell(2,2))

上述代码就将cell(0,0)到cell(2,2)之间的所有cell合并成一个cell

这里需要注意的是,虽然每个cell都合并了,但其实它还是存在的。比如合并了(0,0)和(0,1)两个cell,那么这个合并的cell其实就是(0,0;0,1)

如果cell较多,无法直观的看出坐标的话,可以用下列的代码将每个cell的坐标都标注出来,方便合并

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

hdr_cells0 = table.rows[0].cells

上面代码就得到了合并表格后的第一行所有cell,然后我们用hdr_cell0[0]就可以得到合并表格后的第一行的第一个cell。用add_paragraph方法即可像cell里添加文本

hdr_cells0[0].add_paragraph('数据文字')

其他使用方法可参考官网模块:https://www.osgeo.cn/python-docx/

二、word转成html

1.使用pydocx转换

pip install pydocx

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

<fORM method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="application/vnd.openxmlformats-officedocument.wordprocessingml.document">
</form>

2.使用win32模块

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数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: python读取json数据还原表格批量转换成html

本文链接: https://www.lsjlt.com/news/141265.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作