【问题标题】:How to export pdf with custom name in gui python?如何在 gui python 中使用自定义名称导出 pdf?
【发布时间】:2020-04-30 10:00:28
【问题描述】:

我想为我的 pdf 文件指定一个名称 所以它会询问用户,但我不知道如何接受输入并分配给路径 我尝试使用 asksaveasfilename 但它不保存 所以我需要更改才能单击自定义名称的导出 pdf

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
#import tkintertable 
import tkinter as tk
import tkinter.filedialog
from tkinter.filedialog import asksaveasfilename

def export():
    #x1 = entry1.get()
    with PdfPages(r'C:\Users\Abdul\Desktop\chart1.pdf') as export_pdf:


        feat_importances = pd.Series(model.feature_importances_, index=X.columns)
        feat_importances.nlargest(10).plot(kind='barh')
        Filename = asksaveasfilename( title='Nmae a file', initialdir='C:\\',filetypes=(("PDF file", "*.pdf*"),))
        export_pdf.savefig()
        plt.show()
    plt.close()

button1 = tk.Button (root,text='Export PDF',command=export, bg='Blue', fg='white')
canvas1.create_window(120, 150, window=button1)




root.mainloop()

【问题讨论】:

    标签: python user-interface tkinter save pdf-generation


    【解决方案1】:

    您使用 asksaveasfilename 在正确的行上,但您需要实际使用用户输入的值来设置文件名和路径。

    def export():
        Filename = asksaveasfilename(title='Name a file', initialdir='C:\\', filetypes=(("PDF file", "*.pdf*"),),
                                 defaultextension='.pdf')
        with PdfPages(Filename) as export_pdf:
            feat_importances = pd.Series(model.feature_importances_, index=X.columns)
            feat_importances.nlargest(10).plot(kind='barh')
    
            export_pdf.savefig()
            plt.show()
        plt.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多