如何将Jupyter Notebook中tabulate与IPython.display输出保存为PNG/PDF
如何将Jupyter Notebook中生成的表格与LaTeX公式导出为PNG或PDF?
我在Jupyter Notebook单元格中编写了如下代码,能生成美观的表格和LaTeX公式,但不知道怎么把这些输出内容导出成PNG或PDF文件:
from tabulate import tabulate from IPython.display import Latex, display def display_table(summary, data, best_formula, worst_formula): print(f"\n{summary}\n") print(tabulate(data, headers='keys', tablefmt='fancy_grid')) print(f"\nBest Results Formula:") display(Latex(f'${best_formula}$')) print(f"\nWorst Results Formula:") display(Latex(f'${worst_formula}$')) print("\n" + "-" * 80 + "\n") # Mock data and formulas summary = "Comparison of Different Algorithms" data = [ {"Algorithm": "A", "Accuracy": 0.95, "Precision": 0.93, "Recall": 0.92}, {"Algorithm": "B", "Accuracy": 0.88, "Precision": 0.85, "Recall": 0.84}, {"Algorithm": "C", "Accuracy": 0.80, "Precision": 0.78, "Recall": 0.76} ] best_formula = r'\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}' worst_formula = r'\text{Error Rate} = \frac{FP + FN}{TP + TN + FP + FN}' # Display the table and formulas display_table(summary, data, best_formula, worst_formula)
以下是几种可行的实现方法:
方法一:用Jupyter自带导出功能
- 导出PDF:点击顶部菜单栏
File->Download as-> 选择PDF via LaTeX或PDF via HTML。前者排版更规范,但需要本地安装TeX环境(如MiKTeX、TeX Live);后者兼容性强,无需额外环境,但排版细节可能稍差。 - 导出后会包含整个Notebook的代码和所有输出内容,适合需要完整留存Notebook内容的场景。
方法二:单独导出单元格输出为PNG
如果只想导出目标单元格的表格和公式,可选择以下方式:
- 手动操作:
- 运行单元格后,右键点击渲染好的LaTeX公式,选择
Save Image As可单独保存公式为PNG;表格是文本输出,需要用系统截图工具截取。
- 运行单元格后,右键点击渲染好的LaTeX公式,选择
- 代码自动渲染保存:
借助matplotlib将表格和公式统一渲染成图片,示例代码如下:from tabulate import tabulate import matplotlib.pyplot as plt from matplotlib import rc # 配置LaTeX渲染环境 rc('text', usetex=True) rc('font', family='serif') def save_output_as_image(summary, data, best_formula, worst_formula, save_path="output.png"): # 创建画布和子图 fig, (ax_title_table, ax_best, ax_worst) = plt.subplots(3, 1, figsize=(8, 6)) ax_title_table.axis('off') # 隐藏表格区域坐标轴 # 生成表格数据 table_rows = [[row["Algorithm"], row["Accuracy"], row["Precision"], row["Recall"]] for row in data] table = ax_title_table.table( cellText=table_rows, colLabels=["Algorithm", "Accuracy", "Precision", "Recall"], loc='center' ) # 调整表格样式 table.auto_set_font_size(False) table.set_fontsize(10) table.scale(1.2, 1.2) # 添加标题 ax_title_table.set_title(summary, fontsize=12, pad=20) # 渲染最佳结果公式 ax_best.axis('off') ax_best.text( 0.5, 0.5, f"Best Results Formula:\n${best_formula}$", ha='center', va='center', fontsize=11 ) # 渲染最差结果公式 ax_worst.axis('off') ax_worst.text( 0.5, 0.5, f"Worst Results Formula:\n${worst_formula}$", ha='center', va='center', fontsize=11 ) # 调整子图间距 plt.tight_layout() # 保存图片,dpi控制清晰度 plt.savefig(save_path, dpi=300, bbox_inches='tight') plt.close() # 调用函数保存 save_output_as_image(summary, data, best_formula, worst_formula)- 提前安装依赖:运行
pip install matplotlib;同时需要本地有TeX环境才能正常渲染LaTeX公式。
- 提前安装依赖:运行
方法三:命令行工具nbconvert批量导出
适合自动化或批量处理场景:
- 导出整个Notebook为PDF:
jupyter nbconvert --to pdf your_notebook.ipynb - 先导出为HTML再转PNG(需额外安装
wkhtmltopdf工具):jupyter nbconvert --to html your_notebook.ipynb wkhtmltoimage your_notebook.html output.png
内容的提问来源于stack exchange,提问作者user321627
相关产品推荐
相关产品推荐

