赞
踩
在撰写论文时,使用LaTeX编辑表格往往相当繁琐,需要深入掌握其语法,并参照已有论文模板进行细致调试。而现在,借助PaddleOCR和ERNIE等智能工具,我们可以迅速提取模板论文的表格信息并自动生成规范表格,极大地提升了工作效率和准确性。方便大家快速的对论文表格进行调整。
首先利用PaddleOCR的表格识别功能,对论文里的表格进行识别,然后将结果的html代码进行解析,通过ERNIE转换html代码转换为latex代码。
html代码不需要再过多的处理,可以直接进行转换,十分方便。
准备好论文的表格图片。
安装所需环境,需要安装PaddleOCR,最好从源代码进行安装,目前该项目更新较快,使用新版本更不容易出现错误。源代码安装首先根据requirements.txt安装所需环境以及一些必要的包(使用中报错提示需要安装一些额外的包)
In [ ]
- %cd ~
-
- # 首先建议你先从github上下载PaddleOCR的源码,https://github.com/PaddlePaddle/PaddleOCR.git
- # 先不要使用左侧套件里的PaddleOCR,它的版本使用起来有各种小问题。
- # 我这里先下载好了源代码并上传解压。
- !unzip PaddleOCR-main.zip
-
- # 安装源码所依赖环境
- %cd ~/PaddleOCR-main
- !pip install --user -r requirements.txt
- !pip install --user paddleclas PyMuPDF==1.19.0
- !pip install --user premailer openpyxl
-
- # 运行setup安装
- !python setup.py build install
有两种方法可以使用版面分析和文本识别,一种是命令行运行,一种是python代码运行。
命令行方式运行
In [ ]
- %cd ~/work
-
- !paddleocr --image_dir=table.png --type=structure --layout=false
python脚本方式运行
In [ ]
- import os
- import cv2
- from paddleocr import PPStructure,save_structure_res
-
- table_engine = PPStructure(layout=False, show_log=True)
-
- save_folder = './output'
- img_path = 'table.png'
- img = cv2.imread(img_path)
- result = table_engine(img)
- save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
-
- for line in result:
- line.pop('img')
- print(line)
运行完毕后的文件在work的output文件夹里。
之后使用ERNIE Bot进行转换。
In [ ]
- # 安装ERNIE Bot
- !pip install --upgrade erniebot
设计prompt在后续使用
In [ ]
- # 设计prompt
-
- prompt = (
- "我给你一个html代码,它里面是一个table表格,"
- "帮我转换为latex的代码。"
- "html代码为:"
- )
获得PadedleOCR返回的html数据
In [ ]
- # 表格识别结果文件
- file_path = '/home/aistudio/work/output/table/res_0.txt'
-
- html_list = []
-
- with open(file_path, 'r', encoding='utf-8') as file:
- # 逐行读取文件内容
- for line in file:
- # 尝试将字符串转换为字典
- try:
- data_dict = eval(line)
- # 在这里处理数据
- if data_dict['type'] == 'table' and isinstance(data_dict['res'], dict):
- html = data_dict['res']['html']
- html_list.append(html)
- except Exception as e:
- # 如果转换失败,打印错误信息
- print(f"Error evaluating line in {filename}: {e}")
-

使用ernie-3.5来进行转换,得到转换的结果,获得基本的latex表格代码。
In [ ]
- import erniebot
-
- models = erniebot.Model.list()
- print(models)
-
- # Set authentication params
- erniebot.api_type = "aistudio"
- erniebot.access_token = "你的token"
-
- # 这里用了一个图片,所以list里只有一个list元素
- for html in html_list:
- response = erniebot.ChatCompletion.create(
- model="ernie-3.5",
- messages=[{"role": "user", "content": prompt+html}]
- )
- print(response.get_result())

此项目已发布应用,可以来体验一下。https://aistudio.baidu.com/application/detail/28910
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。