-3

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

4
  • 3
    please, read and follow the guidelines set out in minimal reproducible example , then edit your post to conform, folks may be 'happier' to assist , pastes of images with code are actively discouraged, especially when incomplete - as is your post Commented Jun 8 at 21:54
  • Thanks for pointing this out - hopefully my edited version will make it easier to understand :-) Commented Jun 9 at 6:51
  • Your edit removed important information, for example the use of Tkinter Python library. Commented Jun 9 at 10:53
  • 1
    @Leyth, thanks for pointing this out, now added Commented Jun 9 at 13:22

1 Answer 1

0

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.

4
  • Thanks for your help @Leyth . I'm getting 'TypeError: write() argument must be str, not bytes' but im not sure why. I've simplified it and removed all but the mode='w' and im getting the 'save-as' dialogue box come up ... the file does save but with zero bytes (but Terminal gives me the error message) Commented Jun 9 at 14:37
  • Sorry @Leighski, my mistake, here path_file_name is not the path & name but the file itself. We need to get the path from that object, I will edit my answer. Commented Jun 9 at 15:09
  • @Leighski I edited my answer, please check if it works. Commented Jun 9 at 15:15
  • 1
    you're a star! It works perfectly. Thank you so much. Commented Jun 9 at 16:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.