1

I was wondering how to ask the user for the file location and file name to save the excel file output after using xlswriter.

Currently i have got some code that related to specific location of the code which is not under control of the person using the code. foe example the

workbook=xlswriter.workbook('filepath\filename.xlsx')

i was hoping to find some code where the code pops up a 'savefile name as' and 'save in location' window. thanks!

1
  • you can use tkinter, here is an example of how
    – Nullman
    Commented Apr 30, 2019 at 8:01

2 Answers 2

2

The easiest way is to use the input method:

filename = input("Save file as: ")

This will pop up a message (presuming you're running your script in a terminal or an IDE) asking the user for input, then save their answer to filename. It's up to you whether you want them to input a combination of the extension, the path, or just the name (just modify your instruction so they know what to do).

For example:

filename = input("Save file as: ")

if ".xlsx" not in filename and ".xls" not in filename:
    filename += ".xlsx"

savepath = os.path.join(FOLDER_PATH, filename)

workbook=xlswriter.workbook(savepath)

In this example, you've already defined the path of the folder you want them to save it to and now you're just asking for the filename. Alternatively, you can ask for folder_path too via another input() command.

2

Should this do?

file_name = 'test'
file_path = input('Please enter your save location (ex. filepath\my_folder): ')

workbook=xlswriter.workbook('{}\{}.xlsx'.format(file_path, file_name))
1
  • Thanks!. This code works fine on the output window. i was hoping to create a file 'save as' window which we get while saving excel files or so which would be easier for the user. is there a format in which it could be done? Commented Apr 30, 2019 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.