pythonexcelxlsx

Changing a predefined output name to user-selected name


I'm trying to get Python to export an xlsx file but give me a dialogue box to define the new filename / destination. I have managed to do it to select the initial file to load but unable to work out how to apply this to the processed file:

Currently: dialogue box to select file for Python to process > Python runs the process > exports to a folder/file I've typed into the script.

What I'm trying to do: dialogue box to select file for Python to process > Python runs the process > dialogue box to select destination path and new filename.

#!/usr/bin/env python3 import pandas as pd from openpyxl import load_workbook

import tkinter as tk
from tkinter import filedialog


root = tk.Tk()
root.withdraw()  # This hides the main window
file_path = filedialog.askopenfilename()
print(file_path)

for PANDA CODE:

df = pd.read_excel(file_path)
print(df)


print(df[['Goal Time seconds SERIE B', 'Country SERIE B', 'Match Date SERIE B',]])


df.rename(columns={'Goal Time seconds SERIE B': 'GoalOccurrenceSeconds','Country SERIE B': 'Country', 'Match Date SERIE B': 'MatchDate'}, inplace=True)
print(df[['GoalOccurrenceSeconds', 'Country', 'MatchDate']])
print(df)

data = pd.DataFrame(df[['Country', 'GoalOccurrenceSeconds','MatchDate']])
print(df)

data.to_excel("output1.xlsx", index=False)

#This re-loads the said excel doc ready for 2nd pass at changes.

workbook = load_workbook('output1.xlsx')
sheet = workbook.active

column_to_modify = 'A'
for row in range(2, sheet.max_row + 1):
cell = sheet[f"{column_to_modify}{row}"]
if cell.value == 'ITA':
cell.value = 'TRUE'
elif str(cell.value) != 'ITA':
cell.value = 'FALSE'

This is the bit i can't figure out - to use dialogue box to select folder/filename.

workbook.save('/Users/OUTPUTS/myfilename.xlsx')

Any help gratefully appreciated


Solution

  • You will need to use asksaveasfile function of filedialog. Here is one code that you can use just after the modification done on the .xlsx file and before the workbook.save to get path and name of the file:

    #This line modify the name of the windows opened by tkinter
    window_title = "Please select a folder and a name to save the file"
    #This line allowed you to put a default directory for the user selection
    initialdir = "/your/default/directory/path"
    #This line gives a default name to the file that the user can modify you can give it as None or ""
    initialfile = "YOUR_DEFAULT_FILE_NAME.xlsx"
    #This line gives a default extension to the file
    defaultextension = "xlsx"
    #This line gives the user the allowed extensions
    filetypes = [("Excel Documments","*.xlsx")]
    created_file = filedialog.asksaveasfile(mode='w', window_title = window_title, initialdir = initialdir, initialfile = initialfile, defaultextension = defaultextension, filetypes = filetypes)
    path_file_name = created_file.name
    
    workbook.save(path_file_name)
    

    Only the mode='w' argument is mandatory all the other arguments defined in the code are optional, I only added them because I think they were helpful regarding your needs.