当前位置: 首页 > news >正文

python Matplotlib Tkinter-->导出pdf报表

环境
python:python-3.12.0-amd64
包:
matplotlib 3.8.2
reportlab 4.0.9
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import tkinter as tk
import tkinter.messagebox as messagebox
import tkinter.ttk as ttk
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.units import inch
from reportlab.platypus import Table, TableStyle, SimpleDocTemplate, Image
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from io import BytesIO# 加载中文字体
font_path = r'C:\Windows\Fonts\STZHONGS.TTF'
pdfmetrics.registerFont(TTFont('SimHei', font_path))
print(pdfmetrics.getRegisteredFontNames())# 导出 PDF 报表
def export_pdf():# 将 matplotlib 图形转化为图像buf = BytesIO()fig1.savefig(buf, format='png', dpi=80)buf.seek(0)img = Image(buf, width=8 * inch, height=3 * inch)# 创建 PDF 表格data = [['堆体体积盘点报表', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8'],['名称', '堆体长度', '堆体宽度', '堆体高度', '占地面积', '堆体体积', '堆体密度', '堆体重量'],['1号仓', '36.022', '36.024', '35.063', '1019.495', '28105.247', '0.540', '15176.833'],['制表人:', '', '审核人:', '', '经理:', '', '日期:', '2022-7-9'],['堆体三维立体效果图', '', '', 'row4', '', '', '', ''],[img, '', '', '', 'row5', '', '', ''],['内容7', '', '', '', '', 'row6', '', '']]table = Table(data, colWidths=[1 * inch] * 8, rowHeights=[0.5 * inch] * 5 + [3.1 * inch] * 2)table.setStyle(TableStyle([('TEXTCOLOR', (0, 0), (-1, 0), colors.black),('ALIGN', (0, 0), (-1, 0), 'CENTER'),('FONTNAME', (0, 0), (-1, 0), 'SimHei'),('FONTSIZE', (0, 0), (-1, 0), 14),('BOTTOMPADDING', (0, 0), (-1, 0), 16),('SPAN', (0, 0), (7, 0)),('SPAN', (0, 4), (7, 4)),('SPAN', (0, 5), (7, 5)),('SPAN', (0, 6), (7, 6)),('TEXTCOLOR', (0, 1), (-1, -1), colors.black),('ALIGN', (0, 1), (-1, -1), 'CENTER'),('FONTNAME', (0, 1), (-1, -1), 'SimHei'),('FONTSIZE', (0, 4), (-1, 4), 14),('BOTTOMPADDING', (0, 1), (-1, -1), 16),('GRID', (0, 0), (-1, -1), 1, colors.black)]))# 保存 PDF 文件并显示导出成功的消息框pdf_file = SimpleDocTemplate("三维激光雷达 1号仓体积报表20220709.pdf", pagesize=letter)pdf_file.build([table])messagebox.showinfo('导出成功', 'PDF 文件已成功导出!')# 创建自定义工具栏类
class MyNavigationToolbar(NavigationToolbar2Tk):toolitems = [('Home', '回到初始状态', 'home', 'home'),('Back', '后退', 'back', 'back'),('Home', '前进', 'forward', 'forward'),('Pan', '平移', 'move', 'pan'),('Zoom', '缩放', 'zoom_to_rect', 'zoom'),('Save', '保存', 'filesave', 'save_figure')]def __init__(self, *args, **kwargs):show_toolbar = kwargs.pop('show_toolbar', True)super().__init__(*args, **kwargs)if not show_toolbar:self.pack_forget()self.custom_button_img1 = tk.PhotoImage(file='figure_pic1.png')  # 创建第一个图片按钮self.custom_button1 = ttk.Button(self, image=self.custom_button_img1, command=lambda: print('111.'))self.custom_button1.pack(side=tk.LEFT)  # 添加按钮到工具栏上self.custom_button_img2 = tk.PhotoImage(file='figure_pic2.png')  # 创建第二个图片按钮self.custom_button2 = ttk.Button(self, image=self.custom_button_img2, command=export_pdf)self.custom_button2.pack(side=tk.LEFT)  # 添加按钮到工具栏上# 创建 Tkinter 窗口
window = tk.Tk()
window.title("Matplotlib in Tkinter")# 设置窗口大小和位置
window.geometry('800x600')# 创建选项卡控件
notebook = ttk.Notebook(window)# 创建第一个选项卡
tab1 = ttk.Frame(notebook)
fig1 = plt.figure()
plt.plot([1, 2, 3], [4, 5, 6])
canvas1 = FigureCanvasTkAgg(fig1, master=tab1)
canvas1.draw()
toolbar1 = MyNavigationToolbar(canvas1, tab1)
toolbar1.update()
toolbar1.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
notebook.add(tab1, text='图表1')# 创建第二个选项卡
tab2 = ttk.Frame(notebook)
fig2 = plt.figure()
plt.plot([3, 2, 1], [6, 5, 4])
canvas2 = FigureCanvasTkAgg(fig2, master=tab2)
canvas2.draw()
toolbar2 = MyNavigationToolbar(canvas2, tab2, show_toolbar=False)
toolbar2.update()
canvas2.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
notebook.add(tab2, text='图表2')notebook.pack(fill=tk.BOTH, expand=True)window.mainloop()

 注:

C:\Windows\Fonts\STZHONGS.TTF

STZHONGS.TTF 此名称是文件夹C:\Windows\Fonts\下面的文件属性里面的名称

 

图片资源下载(分享-->python Matplotlib  Tkinter图片):

链接:https://pan.baidu.com/s/1vFOU52gG0bgK8RYuj-dzOg 
提取码:2oy0 

http://www.lryc.cn/news/309510.html

相关文章:

  • react-组件基础
  • The authenticity of host ‘github.com (20.205.243.166)‘ can‘t be established.
  • arduino uno R3驱动直流减速电机(蓝牙控制)
  • 智能家居控制系统(51单片机)
  • 软考高级系统分析师之 URL 知识点和例题
  • vmware虚拟机centos中/dev/cl_server8/root 空间不够
  • C++/数据结构:AVL树
  • Mysql数据库_max_allowed_packet参数详解
  • 【数仓】Hadoop集群配置常用参数说明
  • 【go从入门到精通】什么是go?为什么要选择go?
  • MySQL篇—执行计划介绍(第二篇,总共三篇)
  • nest.js使用nest-winston日志一
  • LeetCode刷题笔记之二叉树(四)
  • 【MATLAB源码-第150期】基于matlab的开普勒优化算法(KOA)机器人栅格路径规划,输出做短路径图和适应度曲线。
  • 最佳实践:Websocket 长连接状态如何保持
  • Unity AStar寻路算法与导航
  • JavaScript最新实现城市级联操作,json格式的数据
  • SD NAND:为车载显示器注入智能与安全的心脏
  • 矩阵的对角化
  • React编写组件时,如何省略.tsx后缀
  • 移动端的React项目中如何配置自适应和px转rem
  • TypeScript 结合 React 开发时候 , React.FunctionComponent 解释
  • 2280. 最优标号(最小割,位运算)#困难,想不到
  • RestTemplate启动问题解决
  • Docker部署前后端服务示例
  • 方格分割644--2017蓝桥杯
  • 接口测试用例设计注意点
  • 学习linux从0到工程师(命令)-4
  • 【树莓派系统配置+python3.8+环境配置踩坑点汇总】raspberrypi
  • CTFHUB--文件包含漏洞--RCE