利用 TableGeneration 生成多样化表格数据
- TableGeneration 简介
- 环境准备
- chrome浏览器(Linux下推荐)
- 火狐浏览器(Mac下推荐)
- 生成表格
- 生成表格
- 参数说明
- 结论
在数据生成和处理领域,表格数据的生成是一个常见需求,尤其是在机器学习和数据分析领域。今天,我们将介绍一个名为 TableGeneration 的工具,它可以帮助我们生成具有多种配置参数的表格数据。这个工具不仅支持生成不同行列数的表格,还可以模拟真实世界中的表格特性,如合并单元格和着色单元格。
TableGeneration 简介
TableGeneration 是一个基于 Python 开发的工具,它利用浏览器渲染技术生成表格图像。这个工具的特点在于其高度的可配置性,用户可以根据需求自定义表格的多种属性。生成的表格可以用于各种场景,如数据分析和机器学习中的数据集构建。
GitHub 页面
环境准备
安装python包
pip3 install -r requirements.txt
目前支持使用chrome浏览器和火狐浏览器,使用方式分别如下
chrome浏览器(Linux下推荐)
- 安装chrome浏览器和中文字体
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
apt-get update && sudo apt-get install libnss3
apt-get install xfonts-wqy
apt install ttf-wqy-zenhei
apt install fonts-wqy-microhei
# refresh fonts
fc-cache -fv
- 安装chrome浏览器驱动 chromedriver
首先在官网下载适合自己系统的驱动文件。然后执行下列命令
unzip chromedriver_linux64.zip
cp chromedriver /usr/local/share/
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
- 测试浏览器和chromedriver
使用如下命令测试chromedriver和chrome浏览器是否安装正确
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https:www.baidu.com')
print(driver.title)
driver.close()
如果成功,会在终端看见如下输出
百度一下,你就知道
火狐浏览器(Mac下推荐)
- 安装火狐浏览器和中文字体
apt-get -y install firefox
apt-get install xfonts-wqy
apt install ttf-wqy-zenhei
apt install fonts-wqy-microhei
# refresh fonts
fc-cache -fv
- 安装火狐浏览器驱动 geckodriver
首先在官网下载适合自己系统的驱动文件。然后执行下列命令
tar -xf geckodriver-v0.31.0-linux64.tar.gz
cp geckodriver /usr/local/share/
ln -s /usr/local/share/geckodriver /usr/local/bin/geckodriver
ln -s /usr/local/share/geckodriver /usr/bin/geckodriver
- 测试浏览器和geckodriver
使用如下命令测试geckodriver和火狐是否安装正确
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(firefox_options=options)
driver.get('https:www.baidu.com')
print(driver.title)
driver.close()
如果成功,会在终端看见如下输出
百度一下,你就知道
生成表格
生成表格
使用如下命令可生成表格,ch_dict_path
和en_dict_path
不指定时,将会使用默认的中英文语料。最终生成的表格图片,表格html文件和PP-Structure格式标注文件会保存在output
指定路径下。
# 简单表格
python3 generate_data.py --output output/simple_table --num=1
# 单元格坐标为单元格内文字坐标的表格
python3 generate_data.py --output output/simple_table --num=1 --cell_box_type='text'
# 彩色单元格表格
python3 generate_data.py --output output/color_simple_table --num=1 --color_prob=0.3
# 清单类表格
python3 generate_data.py --output output/qd_table --num=1 --min_row=10 --max_row=80 --min_col=4 --max_col=8 --min_txt_len=2 --max_txt_len=10 --max_span_row_count=3 --max_span_col_count=3 --max_span_value=20 --color_prob=0 --brower_width=1920 --brower_height=5000
# 大单元格表格
python3 generate_data.py --output output/big_cell_table --num=1 --min_row=6 --max_row=10 --min_col=4 --max_col=8 --min_txt_len=2 --max_txt_len=10 --max_span_row_count=3 --max_span_col_count=3 --max_span_value=10 --color_prob=0 --cell_max_width=100 --cell_max_height=100 --brower_width=1920 --brower_height=1920
参数说明
让我们详细了解一下 TableGeneration 的一些关键参数:
--output output/qd_table
:指定输出目录。这里我们设置输出目录为output/qd_table
。--num=1
:指定生成表格的数量。这里我们只生成一个表格。--min_row=10
和--max_row=80
:设置表格的行数范围。表格将包含 10 到 80 行。--min_col=4
和--max_col=8
:设置表格的列数范围。表格将包含 4 到 8 列。--min_txt_len=2
和--max_txt_len=10
:设置单元格中文本长度的范围。文本长度将在 2 到 10 个字符之间。--max_span_row_count=3
和--max_span_col_count=3
:设置合并单元格的行数和列数范围。合并单元格将最多跨越 3 行和 3 列。--max_span_value=20
:设置合并单元格中的最大数值。这里我们设置最大数值为 20。--color_prob=0
:设置单元格着色的概率。这里我们设置为 0,即不使用颜色。--browser_width=1920
和--browser_height=5000
:设置生成表格的浏览器宽度和高度。这里我们分别设置为 1920 像素和 5000 像素。
通过这些参数,我们可以生成具有特定行列数、文本长度、合并单元格特性以及尺寸的表格。
结论
TableGeneration 是一个强大且灵活的工具,可以用于生成各种类型的表格数据。它的可配置性使得用户能够根据特定需求生成表格,从而在数据分析和机器学习项目中模拟真实世界的表格数据。通过简单的命令行操作,我们可以生成具有不同行列数、文本长度、合并单元格和尺寸的表格,为我们的项目提供丰富的数据支持。
群内交流更多技术
130856474 <-- 在这里