寻求dataframe_image替代方案:生成带颜色标记的表格PNG图片
替代dataframe_image生成带颜色标记表格图片的简便方案
针对你需要生成2列10行、按数值上下限标记单元格颜色的表格图片,且解决PyInstaller打包exe后dataframe_image失效的问题,这里提供两个简便的替代方案:
方案1:Pandas Styler + imgkit(跨平台)
这个方案利用Pandas的样式功能生成带样式的HTML,再通过imgkit将HTML转换为PNG图片,无需依赖Excel,跨平台可用。
步骤:
- 安装依赖:
pip install pandas imgkit
同时下载wkhtmltopdf(开源的HTML转PDF/图片工具),记住其安装路径。
- 代码实现:
import pandas as pd import imgkit import sys import os # 1. 创建测试数据(2列10行) data = { '列1': [12, 35, 8, 42, 25, 5, 30, 18, 45, 22], '列2': [50, 20, 60, 15, 30, 70, 25, 35, 10, 40] } df = pd.DataFrame(data) # 2. 设定数值上下限 lower_limit = 10 upper_limit = 40 # 3. 定义单元格颜色规则 def color_cell(val): if lower_limit <= val <= upper_limit: return 'background-color: #7ccd7c' # 限值内:绿色 else: return 'background-color: #ff6b6b' # 限值外:红色 # 4. 应用样式到DataFrame styled_table = df.style.applymap(color_cell) # 5. 导出为HTML文件 html_file = 'styled_table.html' styled_table.to_html(html_file) # 6. 适配打包环境的wkhtmltopdf路径 if getattr(sys, 'frozen', False): wkhtml_path = os.path.join(sys._MEIPASS, 'wkhtmltopdf.exe') else: wkhtml_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' # 7. 将HTML转换为PNG图片 config = imgkit.config(wkhtmltopdf=wkhtml_path) imgkit.from_file(html_file, 'table_output.png', config=config)
PyInstaller打包注意事项:
- 将wkhtmltopdf.exe与脚本放在同一目录,打包时通过
--add-data包含它:
pyinstaller --onefile --add-data "wkhtmltopdf.exe;." your_script.py
方案2:openpyxl + PyWin32(Windows专属)
如果你的运行环境是Windows且装有Excel,可通过openpyxl生成带样式的Excel文件,再通过PyWin32调用Excel将表格区域导出为图片。
步骤:
- 安装依赖:
pip install pandas openpyxl pywin32
- 代码实现:
import pandas as pd from openpyxl import Workbook from openpyxl.styles import PatternFill from openpyxl.utils.dataframe import dataframe_to_rows import win32com.client as win32 import os # 1. 创建数据与上下限 data = { '列1': [12, 35, 8, 42, 25, 5, 30, 18, 45, 22], '列2': [50, 20, 60, 15, 30, 70, 25, 35, 10, 40] } df = pd.DataFrame(data) lower_limit = 10 upper_limit = 40 # 2. 创建Excel并写入数据 wb = Workbook() ws = wb.active for row in dataframe_to_rows(df, index=False, header=True): ws.append(row) # 3. 定义填充颜色 green_fill = PatternFill(start_color='7ccd7c', end_color='7ccd7c', fill_type='solid') red_fill = PatternFill(start_color='ff6b6b', end_color='ff6b6b', fill_type='solid') # 4. 遍历单元格设置颜色(跳过表头,处理10行数据) for row in ws.iter_rows(min_row=2, max_row=11, min_col=1, max_col=2): for cell in row: if lower_limit <= cell.value <= upper_limit: cell.fill = green_fill else: cell.fill = red_fill # 5. 保存Excel文件 excel_path = 'styled_table.xlsx' wb.save(excel_path) # 6. 调用Excel导出表格为图片 excel = win32.gencache.EnsureDispatch('Excel.Application') excel.Visible = False # 后台运行Excel wb = excel.Workbooks.Open(os.path.abspath(excel_path)) ws = wb.Worksheets(1) # 调整列宽保证内容完整 ws.Columns(1).ColumnWidth = 10 ws.Columns(2).ColumnWidth = 10 # 复制表格区域并导出为PNG table_range = ws.Range(ws.Cells(1, 1), ws.Cells(11, 2)) table_range.CopyPicture(Format=win32.constants.xlPicture) ws.Shapes.AddPicture().Select() excel.Selection.Paste() ws.Shapes(ws.Shapes.Count).Export('table_output.png') # 清理资源 wb.Close(SaveChanges=False) excel.Quit() os.remove(excel_path) # 可选:删除临时Excel文件
PyInstaller打包注意事项:
- 打包时需添加隐藏依赖:
pyinstaller --onefile --hidden-import win32com.client your_script.py
内容的提问来源于stack exchange,提问作者toben aus
相关产品推荐
相关产品推荐

