创建.xlsx文件,然后通过将其保存为对话框,例如-python3

发布于 2025-02-03 02:28:45 字数 10137 浏览 2 评论 0原文

我在编程方面相对崭新,并且在搜索和尝试几个小时后无法解决一个问题。.

所以我愿意的是..我有一个可以做一些测量工作的程序。 我想做的是将这些结果导出到

创建Excel文件的Excel文件(.xlsx)中,这不是XLSWRITER模块的大问题。 如果我不选择.xlsx文件的路径,它实际上将其保存在我脚本所在的目录中。而且,如果我想选择一条路径,我可以在Skript中很容易地添加。

但是现在,我希望这样做的用户实际上可以选择要保存.xlsx文件的位置。我猜应该用TKINTER模块存档(IM打开用于不同的解决方案)。因此,使用TKINTER模块,我可以打开一个对话框,用户可以在其中选择目录而无需编辑/修改实际的SKRIPT,这使其成为用户。

因此,我已经编码了一些我认为实际上工作的行。 在我的Skript的第一部分中,它创建了一个工作簿,并在里面写了一些东西并关闭它。 在第二部分中,保存文件的对话框将显示,我可以选择保存.xlsx文件的位置。 但是问题是,对话框实际上不能保存第一部分中创建的.xlsx文件。 它仅保存一个新的.xlsx文件,该文件是空的,我什至无法打开。

因此,必须有一种方法.. tkinter dialaog实际上从我的skript的第一部分或我创建的实际工作簿中抓住了.xlsx文件,然后将其放入tkinter对话框中,以便我可以将.xlsx文件保存在用户的位置。想要。

有人有任何想法还是可以帮助我?

哦,如果您在这里写一个问题有任何尖刻,也很感激,谢谢! :)

Skript:

#Import necessary Library                                                                                                                                                                  
import xlsxwriter                                                                                                                                                                          
from tkinter import *                                                                                                                                                                      
from tkinter import filedialog                                                                                                                                                             
                                                                                                                                                                                           
                                                                                                                                                                                           
savelocation = r'\Path'                                                                                                                                                                    
                                                                                                                                                                                           
                                                                                                                                                                                           
#Create a workbook and add a worksheet.                                                                                                                                                   
workbook = xlsxwriter.Workbook (saveloaction)                                                                                                                                              
worksheet = workbook.add_worksheet()                                                                                                                                                       
                                                                                                                                                                                           
#Some data we want to write to the worksheet.                                                                                                                                             
expenses = (                                                                                                                                                                               
    ['Rent', 1000],                                                                                                                                                                        
    ['Gas',   100],                                                                                                                                                                        
    ['Food',  300],                                                                                                                                                                        
    ['Gym',    50],                                                                                                                                                                        
)                                                                                                                                                                                          
                                                                                                                                                                                           
#Start from the first cell. Rows and columns are zero indexed.                                                                                                                            
row = 0                                                                                                                                                                                    
col = 0                                                                                                                                                                                    
                                                                                                                                                                                           
#Iterate over the data and write it out row by row.                                                                                                                                       
for item, cost in (expenses):                                                                                                                                                              
    worksheet.write(row, col,     item)                                                                                                                                                    
    worksheet.write(row, col + 1, cost)                                                                                                                                                    
    row += 1                                                                                                                                                                               
                                                                                                                                                                                           
#Write a total using a formula.                                                                                                                                                           
worksheet.write(row, 0, 'Total')                                                                                                                                                           
worksheet.write(row, 1, '=SUM(B1:B4)')                                                                                                                                                     
                                                                                                                                                                                           
workbook.close()                                                                                                                                                                           
                                                                                                                                                                                           
#open file.                                                                                                                                                                               
                                                                                                                                                                                           
#win = Tk()                                                                                                                                                                                
#win.withdraw()                                                                                                                                                                            
#openfile = filedialog.askopenfile(initialfile='Results.xlsx', initialdir=r'C:\Users\Bikes\Desktop\Results.xlsx' , filetypes=(('Excel File', '.xlsx'),('All Files','*.*')) )               
                                                                                                                                                                                           
#save file with dialog.                                                                                                                                                                   
                                                                                                                                                                                           
win = Tk()                                                                                                                                                                                 
win.withdraw()                                                                                                                                                                             
#fileopen = filedialog.Open = "Results.xlsx"                                                                                                                                               
filename = filedialog.asksaveasfilename(initialfile='Results.xlsx', initialdir='/', title = 'Save File', filetypes=(('Excel File', '.xlsx'),('Text File','.txt'),('All Files','*.*')))     
                                                                                                                                                                                           

I´m relative new in programming and im facing a issue i cant solve after hours of searching and trying..

So what i would like to is.. i have a program which does do some measurement stuff.
What i want to do is to export these results into a excel file (.xlsx)

Creating the excel file is not a big problem with the xlswriter module.
If i don't choose a path for the .xlsx file it actually saves it in the directory where my scripts are. And if i want to choose a path i can add that very easily in my skript.

But now i want that the User that does do the meaurements can actually choose where the .xlsx file will be saved. This should be archived with the tkinter module i guess (im open for different solutions). So with the tkinter module i can open a Dialog where the User can choose the directory without editing/modifying the actual skript, which makes it User-frendly.

So i already have coded some lines which is actually close to work i think.
In the first part of my skript it creates a workbook and writes some stuff inside and closes it.
In the second part the Dialog to save the file will show up and i can choose the location where my .xlsx file will be saved.
But the Problem is, that the Dialog actually dont save the .xlsx file that was created in the first part.
It only saves a new .xlsx file which is empty and i cant even open.

So there must be a way.. where the tkinter Dialaog actually grabs the .xlsx file from the first part of my skript or from the actual workbook i created and put that into the tkinter Dialog so i can save the .xlsx file where the User wants to.

Does someone have any idea or can help me?

Oh and if you have any tipps regarding writing a question here it also appreciated, thanks! :)

skript:

#Import necessary Library                                                                                                                                                                  
import xlsxwriter                                                                                                                                                                          
from tkinter import *                                                                                                                                                                      
from tkinter import filedialog                                                                                                                                                             
                                                                                                                                                                                           
                                                                                                                                                                                           
savelocation = r'\Path'                                                                                                                                                                    
                                                                                                                                                                                           
                                                                                                                                                                                           
#Create a workbook and add a worksheet.                                                                                                                                                   
workbook = xlsxwriter.Workbook (saveloaction)                                                                                                                                              
worksheet = workbook.add_worksheet()                                                                                                                                                       
                                                                                                                                                                                           
#Some data we want to write to the worksheet.                                                                                                                                             
expenses = (                                                                                                                                                                               
    ['Rent', 1000],                                                                                                                                                                        
    ['Gas',   100],                                                                                                                                                                        
    ['Food',  300],                                                                                                                                                                        
    ['Gym',    50],                                                                                                                                                                        
)                                                                                                                                                                                          
                                                                                                                                                                                           
#Start from the first cell. Rows and columns are zero indexed.                                                                                                                            
row = 0                                                                                                                                                                                    
col = 0                                                                                                                                                                                    
                                                                                                                                                                                           
#Iterate over the data and write it out row by row.                                                                                                                                       
for item, cost in (expenses):                                                                                                                                                              
    worksheet.write(row, col,     item)                                                                                                                                                    
    worksheet.write(row, col + 1, cost)                                                                                                                                                    
    row += 1                                                                                                                                                                               
                                                                                                                                                                                           
#Write a total using a formula.                                                                                                                                                           
worksheet.write(row, 0, 'Total')                                                                                                                                                           
worksheet.write(row, 1, '=SUM(B1:B4)')                                                                                                                                                     
                                                                                                                                                                                           
workbook.close()                                                                                                                                                                           
                                                                                                                                                                                           
#open file.                                                                                                                                                                               
                                                                                                                                                                                           
#win = Tk()                                                                                                                                                                                
#win.withdraw()                                                                                                                                                                            
#openfile = filedialog.askopenfile(initialfile='Results.xlsx', initialdir=r'C:\Users\Bikes\Desktop\Results.xlsx' , filetypes=(('Excel File', '.xlsx'),('All Files','*.*')) )               
                                                                                                                                                                                           
#save file with dialog.                                                                                                                                                                   
                                                                                                                                                                                           
win = Tk()                                                                                                                                                                                 
win.withdraw()                                                                                                                                                                             
#fileopen = filedialog.Open = "Results.xlsx"                                                                                                                                               
filename = filedialog.asksaveasfilename(initialfile='Results.xlsx', initialdir='/', title = 'Save File', filetypes=(('Excel File', '.xlsx'),('Text File','.txt'),('All Files','*.*')))     
                                                                                                                                                                                           

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文