You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用FPDF生成PDF时弹出另存为对话框自定义保存路径

问题解决方法

你当前代码使用tkinter搭建GUI,直接调用tkinter内置的filedialog模块即可实现「另存为」对话框功能,不需要安装额外第三方依赖。

具体修改点

  • 在代码顶部的导入区域新增filedialog模块的导入语句
  • 替换原有固定文件名直接保存PDF的逻辑:先弹出另存为对话框,预设默认文件名、PDF格式筛选规则,检测到用户选择有效保存路径后再执行写入操作,用户点击取消时直接终止保存流程避免报错。

修改后的完整实现代码

# 把下方导入语句放到代码顶部的导入区,和原有openpyxl/numpy/FPDF/messagebox等导入放在一起
from tkinter import filedialog

# 你原有GUI组件定义、业务变量等代码全部保持不变即可

def Save_doses(spacing=1):
    try:
        wb=openpyxl.load_workbook("Effective Dose-Stylized.xlsx")  
        A1=float(activity1.get())
        male=[]
        female=[]
        sheet=wb[isotopes1.get()]   
        for i in range(2,44):
            maledose=sheet.cell(row=i,column=2).value
            femaledose=sheet.cell(row=i,column=3).value
            male.append(maledose)
            female.append(femaledose)
        dose_male=A1*np.array(male)
        dose_female=A1*np.array(female)
        Edmale26=sheet.cell(row=44, column=2).value
        Edfemale26=sheet.cell(row=44, column=3).value
        Edmale60=sheet.cell(row=45, column=2).value
        Edfemale60=sheet.cell(row=45, column=3).value
        Ed103=sheet.cell(row=46, column=2).value
        header=[...] # 原有表头内容保持不变
        data = [...] # 原有表格数据内容保持不变

        pdf = FPDF()
        pdf.set_font("Times", size=12)
        pdf.add_page()

        col_width = pdf.w / 4.5
        row_height = pdf.font_size
        for row in header:
            for item in row:
                pdf.cell(col_width, row_height*spacing,
                         txt=item)
            pdf.ln(row_height*spacing)
        for row in data:
            for item in row:
                pdf.cell(col_width, row_height*spacing,
                         txt=item, border=1, align="C")
            pdf.ln(row_height*spacing)  
        
        # ----- 替换原有固定路径保存的新逻辑开始 -----
        # 生成默认显示的文件名,和你原有命名规则一致
        default_filename = f"{radionuclidename.get()} , {radiopharmaname.get()}.pdf"
        # 弹出系统原生另存为对话框
        save_path = filedialog.asksaveasfilename(
            defaultextension=".pdf",
            filetypes=[("PDF 文件", "*.pdf")],
            initialfile=default_filename,
            title="选择PDF保存位置"
        )
        # 用户点击取消按钮时返回空路径,直接终止流程不报错
        if not save_path:
            return
        # 将PDF写入用户选择的目标路径
        pdf.output(save_path)
        # 可选:保存完成后弹出成功提示
        messagebox.showinfo(message="PDF文件保存成功", icon="info")
        # ----- 修改部分结束 -----
        
    except ValueError:
        messagebox.showinfo(message='you must enter an initial activity in MBq', icon="error")
        pass

参数说明

  • defaultextension=".pdf":用户未手动输入文件后缀时,自动补全.pdf后缀
  • filetypes=[("PDF 文件", "*.pdf")]:对话框默认只筛选显示PDF格式文件,避免用户选错保存格式
  • initialfile=default_filename:对话框打开时自动填充你原有规则生成的文件名,用户不需要重复输入

内容的提问来源于stack exchange,提问作者Farzin Safarnejad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 23:42:19