Download spreadsheet with Openpyxl

Asked

Viewed 384 times

-1

I’m struggling to resolve this issue. I’ve done a lot of research and haven’t found a solution to the problem yet.

In Python, I use the Openpyxl library to generate a simple spreadsheet in Excel. Saved in the local directory and everything works fine with the code below:

wb = Workbook()

 ws = wb.create_sheet(‘Dados’)

ws['A1'] = 'Nome'

ws['A2'] = 'Telefone'

wb.save()

However, I would like the moment I was saving this spreadsheet, instead of saving it in the application directory, to open the "Save As" window for the user to choose where he would like to save his spreadsheet.

Because this spreadsheet cannot be saved in the application directory, either local or on the server, but the user will choose his preferred location to record.

  • I believe you use Django once it is in the tag. So, I ask you to put the.py urls, views.py and template if it exists.

  • The openpyxl does not perform excel. It creates the file structure in excel format alone. openpyxl It is not a graphical tool, it does not display anything on your screen. If you want to show a "Save As" window, you will need to use some user interface library, such as tkinter, gtk, kivy, etc.

  • Yes it is true, it does not run Excel. Maybe I could not explain. In my application, I have an "Export" button that calls that function and that would be the magic. I would like that the moment this button was clicked, this Excel was created the structure and then gives the option to the user in which location he would like to save as if it were a dowaload procedure. I don’t know if I could help with the enlightenment.

  • Your question is very bad, it focuses on openpyxl that has nothing to do with it. What you want to learn is how to show a user question in HTML/Javascript in the browser, and receive the value from the server side

  • That’s why you’re getting these unrelated answers. I suggest improving the question

3 answers

0

Good afternoon Leandro, all right with you?

As the staff commented, the tkinter is a good option, it usually comes already installed in basic Python, so if it is an application that runs locally (on the user’s PC and not in the browser) you can use it.

from tkinter import filedialog

filename = filedialog.asksaveasfilename()

print(filename)

filename will be the file path chosen by the user, I believe save from Workbook can receive the path. From a documentation and run some tests.

  • The idea is very good. But the application runs on a server and not on site. I understood that this would be a local solution just right!?

0

a possible solution is you set the location where you want to save your file.

follows an example I hope I can help you.

from openpyxl import Workbook
wb = Workbook()

ws = wb.create_sheet('Dados')

ws['A1'] = 'Nome'

ws['A2'] = 'Telefone'

"""
* o nome do arquivo já deve ser seguido por .xlsx
* o path é o local onde você deseja salvar o arquivo. Informe
  o caminho e inverta as barras.

"""
nome_arquivo = input(str('Digite um nome para o arquivo'))
path = input(str('Onde deseja salvar?'))
wb.save(path+nome_arquivo)
  • It would be a good output your solution. But the application works on a server and ai would not be able to save in the user’s location.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.