Web Development

Our forums are currently in maintenance mode and the ability to post is disabled. We will be back up and running as soon as possible. Thanks for your patience!

Question

Resolved

How to Use an HTML Excel File Input

By bgilley227 ·
My research team is building a website that requires the user to input an Excel file (.xlsx) with 6 different sheets. Each sheet has a different quantity of data. I understand that to create a file input button, you use the following format:

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

However, I'm not sure how to process the data inside of the Excel file the user inputs. We are using Python's flask module as the web framework, so ideally, the contents of each sheet would be loaded into dictionaries on the flask file. I am not sure how to extract the data from each sheet of the file they input. Any help is appreciated. Thanks!
best answer
Collapse -

Your developer could use Pandas (that Python library.)

by rproffitt Moderator In reply to How to Use an HTML Excel ...

Your posts so far tell me about your use of Flask but why HTML given the story? Why not Pandas to get inside the Excel sheets?

Tutorial at https://www.dataquest.io/blog/excel-and-pandas/

But let's back up to the beginning. I feel you haven't completed your System Design. This is something I see missing in many CompSci paths today. We used to call it Systems Analysis but for here you have the Flask, the Excel file and now need to complete your System Design.

BREAK IT DOWN INTO MANAGEABLE STEPS.
1. The web page that the user sees and uploads the file.
2. The backend that places the file where you feel it needs to be.
3. The other backend (Python + Pandas) to pull out the data to the format that is needed for the next step.
Continue with more steps.

If something is too hard, break it down into more steps. Repeat the break down until it's something you or your programmers can handle.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Re: spreadsheet

by Kees_B Moderator In reply to How to Use an HTML Excel ...

That's a file upload. Just save the file and use any program on the server (like MS Excel or LibreOffice Calc) to read and process it. That might need some macro's.

Collapse -

Reply to Kees_B

by bgilley227 In reply to Re: spreadsheet

This is for a website with no system operator or anything like that. When the user inputs the file, it has to be automatically processed and put into Python dictionaries to be sent to the back end for calculations. I'm just not sure how to go from the input button to all of the contents of the file being put into Python dictionaries. Is there a way to work with the HTML input directly, or could the file automatically be saved to the directory, and then another function could be written to work with that file? Thanks

Collapse -

Re: save

by Kees_B Moderator In reply to Reply to Kees_B

Yes, it's easy to save the file (the sites I read talk about a save-method, but I can't give the details) in a folder. I wouldn't call that processing.

And once it's in a folder the OS can do with it what you want. Use at in Linux or the task scheduler in Windows.

But why not save it directly in a folder the backend can read?
Or, even easier maybe, give each user an ftp-script that copies his spreadsheet to some folder near the backend. Why do you need an intermediate site?

Collapse -

Re

by bgilley227 In reply to Re: save

It would certainly work if the file was saved directly in a folder that the backend can read. I'm just not sure how to take the HTML file input and save it to a directory on the server. Any tips with this? I could be searching for the wrong things, but I can't find much info on this, and what I do find involves a lot of JavaScript (which seems unnecessary for such a common task).

Collapse -

Re: save file

by Kees_B Moderator In reply to Re

https://flask.palletsprojects.com/en/2.0.x/patterns/fileuploads/ gives example code to upload and save a file in Flask.

Worth a try, I'd say.

Collapse -

Reply to rproffitt

by bgilley227 In reply to You developer could use P ...

Thanks for the response. My team has already created a function that can read the Excel file that the user inputs on the HTML page. The Excel file will always be in the same format, so we have used Pandas to read the file. Right now, this function references a template Excel file that is in the same directory. In practice, the Pandas function won't read the template file. It will read the file that the user inputs. Our problem is that we do not know how to save the Excel file that the user inputs to the same directory as the Pandas function (or any directory for that matter). We do not understand how the Excel file that is input using a submit button in HTML (with type="file") can be saved to a specific directory. Thanks for the help

Collapse -

OK so the problem right now is

by rproffitt Moderator In reply to Reply to rproffitt

Where is that file that was uploaded?

That varies with the webserver setup. Example:
https://pythonbasics.org/flask-upload-file/#:~:text=Each%20uploaded%20file%20is%20first,files%20object.

There are many other tutorials on configuring where the uploaded file goes.

Collapse -

Re

by bgilley227 In reply to OK so the problem right n ...

This helped. Had to edit the HTML form and the Flask file. Thanks!

Related Discussions

Related Forums