[英]How to export pdf with custom name in gui python?
我想為我的 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()
您使用 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.