来源:互联网 更新时间:2026-06-10 14:10
说到表格数据合成,尤其是基于LaTeX的渲染方案,其实核心思路并不复杂,但有几个关键的坎儿必须迈过去。今天就结合GOT-OCR这条路子,把整个流程掰开揉碎了聊聊。
先得说清楚:环境得先搭好,不然下面这些操作都无从谈起。

准备工作其实很简单,用下面这个LaTeX模板就够了。这次咱们只合成表格,所以模板不用太花哨。
documentclass[10pt]{article}
usepackage[top=.5in,bottom=1in,left=.5in,right=.5in]{geometry}
usepackage[UTF8]{ctex}
usepackage[pagebackref=true,breaklinks=true,colorlinks,bookmarks=false]{hyperref}
usepackage{amsmath,amsfonts,mathrsfs,amssymb}
% 设置中文字体
usepackage{multirow}
renewcommand{normalsize}{fontsize{6pt}{6pt}selectfont}
pagestyle{empty} % 去掉页码
setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei]
begin{document}
begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
hline
& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \
hline
品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \
hline
& 1 & 0.86 & 0.81 & 0.53 & 0.73 & 0.80 & 0.22 & 0.69 & 0.80 & 0.15 & 0.70 & 0.78 & 0.26 & 0.72 & 0.76 & 0.31 \
cline{2-17}
& 2 & 0.64 & 0.69 & 0.56 & 0.57 & 0.65 & 0.20 & 0.46 & 0.69 & 0.19 & 0.59 & 0.69 & 0.27 & 0.54 & 0.65 & 0.22 \
cline{2-17}
& 3 & 0.48 & 0.63 & 0.57 & 0.48 & 0.50 & 0.23 & 0.39 & 0.63 & 0.20 & 0.47 & 0.61 & 0.21 & 0.47 & 0.61 & 0.22 \
cline{2-17}
& 4 & 0.37 & 0.59 & 0.60 & 0.42 & 0.52 & 0.24 & 0.33 & 0.57 & 0.21 & 0.34 & 0.54 & 0.18 & 0.40 & 0.58 & 0.24 \
cline{2-17}
& 5 & 0.31 & 0.56 & 0.61 & 0.36 & 0.47 & 0.23 & 0.25 & 0.52 & 0.20 & 0.28 & 0.48 & 0.20 & 0.32 & 0.52 & 0.26 \
hline
& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \
hline
品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \
hline
& 1 & 0.85 & 0.77 & 0.47 & 0.87 & 0.81 & 0.56 & 0.74 & 0.81 & 0.13 & 0.88 & 0.85 & 0.60 & 0.72 & 0.82 & 0.19 \
cline{2-17}
& 2 & 0.64 & 0.65 & 0.43 & 0.60 & 0.64 & 0.55 & 0.55 & 0.68 & 0.16 & 0.71 & 0.76 & 0.59 & 0.54 & 0.69 & 0.18 \
cline{2-17}
& 3 & 0.50 & 0.58 & 0.49 & 0.42 & 0.59 & 0.55 & 0.45 & 0.59 & 0.18 & 0.59 & 0.70 & 0.63 & 0.40 & 0.62 & 0.16 \
cline{2-17}
& 4 & 0.38 & 0.58 & 0.53 & 0.37 & 0.56 & 0.54 & 0.37 & 0.54 & 0.19 & 0.49 & 0.65 & 0.68 & 0.36 & 0.56 & 0.15 \
cline{2-17}
& 5 & 0.30 & 0.55 & 0.56 & 0.24 & 0.54 & 0.58 & 0.32 & 0.49 & 0.18 & 0.35 & 0.60 & 0.68 & 0.28 & 0.48 & 0.14 \
hline
end{tabular}
end{document}
把上面的内容存成tmp.tex文件,然后运行下面这条命令:
lualatex -output-directory=./tmp -interaction=nonstopmode --shell-escape tmp_copy.tex
就能得到渲染后的PDF文件,效果大致如下:
先别急着往下看,这里抛几个问题,可以琢磨琢磨,如果是你,会怎么解决?
下面,咱们就围绕这三个核心问题,掰开揉碎了聊一聊。
最直接能想到的多样性,大概有这几点:
其中,
至于
setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei],这就是设置字体的关键。这下明白了吧?想让字体花样多,很简单:颜色这块,如果
最直接的办法,就是把PDF转成图片,再用二值化的方法把表格提取出来。OpenCV里提供了好几种二值化方法,比如:
实际操作下来,
import fitz # PyMuPDF
import numpy as np
import matplotlib.pyplot as plt
import cv2
def pdf_to_images_with_bbox(pdf_path):
# 打开PDF文件
pdf_document = fitz.open(pdf_path)
for page_num in range(len(pdf_document)):
# 获取当前页
page = pdf_document.load_page(page_num)
# 将页面转换为PIL图像
pix = page.get_pixmap(dpi=288)
img = np.frombuffer(buffer=pix.samples, dtype=np.uint8).reshape((pix.height, pix.width, 3))
image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# 应用自适应阈值化
max_value = 255
adaptive_method = cv2.ADAPTIVE_THRESH_MEAN_C # 或 cv2.ADAPTIVE_THRESH_GAUSSIAN_C
block_size = 11
C = 2
binary_image = cv2.adaptiveThreshold(image, max_value, adaptive_method, cv2.THRESH_BINARY, block_size, C)
print(binary_image.shape)
y = np.sum(255 - binary_image, axis=-1)
x = np.sum(255 - binary_image, axis=0)
x = np.where(x>=1)[0]
y = np.where(y >= 1)[0]
x_min, x_max = x[0], x[-1]
y_min, y_max = y[0], y[-1]
print([x_min, y_min, y_min, y_max])
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (0,0, 255), 3)
plt.imshow(img)
plt.show()
# 示例用法
pdf_path = "./tmp.pdf"
pdf_to_images_with_bbox(pdf_path)
注:截图的时候,建议四边随机往外扩一点。贴着边把图截下来,效果其实不太好。
理解了上面这些,自动化就水到渠成了。可以把LaTeX代码拆成三段来看:
documentclass[10pt]{article}
usepackage[top=.5in,bottom=1in,left=.5in,right=.5in]{geometry}
usepackage[UTF8]{ctex}
usepackage[pagebackref=true,breaklinks=true,colorlinks,bookmarks=false]{hyperref}
usepackage{amsmath,amsfonts,mathrsfs,amssymb}
% 设置中文字体
usepackage{multirow}
renewcommand{normalsize}{fontsize{6pt}{6pt}selectfont}
pagestyle{empty} % 去掉页码
这里的字体大小,可以通过后处理图像时的dpi来控制,所以这部分直接固定不变就好。
setCJKmainfont{SimSun}[BoldFont=KaiTi, ItalicFont=SimHei]
这里只给出了字体设置的全局变量。如果对LaTeX很熟,可以把所有可变的部分都放在这里。为了配合Python代码,可以这么改写:
fonts = ["字体1","字体2","字体3",..., "字体n"]
temp = f"\setCJKmainfont{{{font1}}}[BoldFont={font2}, ItalicFont={font3}]"
set_font = temp.format(font1=random.choice(fonts),
font2=random.choice(fonts),
font3=random.choice(fonts),)
这样一来,set_font就有了随机性。
temp = f"""\begin{{document}}
{tabular}
\end{{document}}
"""
tabular_text = r"""
begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
hline
& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \
hline
品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \
hline
& 1 & 0.86 & 0.81 & 0.53 & 0.73 & 0.80 & 0.22 & 0.69 & 0.80 & 0.15 & 0.70 & 0.78 & 0.26 & 0.72 & 0.76 & 0.31 \
cline{2-17}
& 2 & 0.64 & 0.69 & 0.56 & 0.57 & 0.65 & 0.20 & 0.46 & 0.69 & 0.19 & 0.59 & 0.69 & 0.27 & 0.54 & 0.65 & 0.22 \
cline{2-17}
& 3 & 0.48 & 0.63 & 0.57 & 0.48 & 0.50 & 0.23 & 0.39 & 0.63 & 0.20 & 0.47 & 0.61 & 0.21 & 0.47 & 0.61 & 0.22 \
cline{2-17}
& 4 & 0.37 & 0.59 & 0.60 & 0.42 & 0.52 & 0.24 & 0.33 & 0.57 & 0.21 & 0.34 & 0.54 & 0.18 & 0.40 & 0.58 & 0.24 \
cline{2-17}
& 5 & 0.31 & 0.56 & 0.61 & 0.36 & 0.47 & 0.23 & 0.25 & 0.52 & 0.20 & 0.28 & 0.48 & 0.20 & 0.32 & 0.52 & 0.26 \
hline
& & multicolumn{3}{|c|}{textbf{参考情景}} & multicolumn{3}{|c|}{textbf{情景1}} & multicolumn{3}{|c|}{textbf{情景2}} & multicolumn{3}{|c|}{textbf{情景3}} & multicolumn{3}{|c|}{textbf{情景4}} \
hline
品种 & G & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} & textbf{BV} & textbf{Add} & textbf{Dom} \
hline
& 1 & 0.85 & 0.77 & 0.47 & 0.87 & 0.81 & 0.56 & 0.74 & 0.81 & 0.13 & 0.88 & 0.85 & 0.60 & 0.72 & 0.82 & 0.19 \
cline{2-17}
& 2 & 0.64 & 0.65 & 0.43 & 0.60 & 0.64 & 0.55 & 0.55 & 0.68 & 0.16 & 0.71 & 0.76 & 0.59 & 0.54 & 0.69 & 0.18 \
cline{2-17}
& 3 & 0.50 & 0.58 & 0.49 & 0.42 & 0.59 & 0.55 & 0.45 & 0.59 & 0.18 & 0.59 & 0.70 & 0.63 & 0.40 & 0.62 & 0.16 \
cline{2-17}
& 4 & 0.38 & 0.58 & 0.53 & 0.37 & 0.56 & 0.54 & 0.37 & 0.54 & 0.19 & 0.49 & 0.65 & 0.68 & 0.36 & 0.56 & 0.15 \
cline{2-17}
& 5 & 0.30 & 0.55 & 0.56 & 0.24 & 0.54 & 0.58 & 0.32 & 0.49 & 0.18 & 0.35 & 0.60 & 0.68 & 0.28 & 0.48 & 0.14 \
hline
end{tabular}
"""
table_text = temp.format(tabular=tabular_text)
完成上面
第一段 + 第二段 + 第三段,就能得到一个完整的、可渲染的LaTeX文本。
以上就是自动生成多样化表格数据集的一个基础流程。当然,实际渲染过程中还有很多细节需要自己去摸索处理。比如:
等等。遇到问题动手去调,才能真正把这条路走通。
下饭影视APP下载安装指南
和平精英如何做到压枪稳-和平精英怎样才能压枪稳
《Off Campus》第二季官宣:这对CP还在,但不再是主角
下载浏览器app下载安装选择推荐
免费影视剧APP推荐
儿子穿新中式现身大会堂 马斯克罕见用中文回应:他正在学习普通话
Elysium Above 履云录官网在哪下载 最新官方下载安装地址
抖音最火沙雕男生网名(精选100个)
阿里发布Qwen3.7-Max大模型,全球第五、国产第一
网络热词聊污是什么意思
名单曝光!库克、马斯克等将随团到访中国 黄仁勋不在其中
短剧《情绪超市》剧情介绍
短视频软件推荐
洛克王国世界S2赛季狂欢怪谈介绍
免费看电影的软件推荐
SpaceX狂揽AI人才,马斯克亲自面试且不看简历背景
HBO 奇幻剧《龙之家族》第三季定档 6 月 22 日,最终预告片曝光喉道海战
KuCoin基本面分析
金铲铲之战s17六暗星卡莎阵容玩法构筑指南
苹果macOS 27将优化界面设计并测试AI驱动的Safari标签页自动分组功能
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc