I have been given a task to create a Python file that will take the data from an HTML table and export the data into an Excel sheet.
I have the Python file created and want to test it now. I have created the HTML with a sample table with data in it.
I have to create javascript to take the table data and send it to the Python file for processing. I am using a button to set the action.
Javascript
$("button").click(
function(){
//What should go here?
}
);
You probably don’t need it but if you want to see the HTML I can post it as well, but it is really just a table and a button. Pretty straight forward. The Python file is pretty complex so I’d rather not post it. I just need to know how to get the table data and export it to the Python file for processing.
Thanks!
To clarify. I am hard-coding a table in HTML for testing purposes. A sample user should be able to go to the web page and view the table. Then have the option to export the table into an excel sheet to save on their computer. So I have created a button “Export” to start this process, but don’t know how to get the hard-coded data to the python script
I see two parts to this problem:
Extracting data from HTML table
I’ve not actually done this before, but there are answers already on StackOverflow that explain several ways of doing with the aid of Python libraries like lxml or BeautifulSoup.
The easiest way of doing this is actually to avoid doing it at all. Will you have access to the data that was used to create the HTML table, and can you use that directly instead?
Exporting data to a spreadsheet
The Excel file formats (
.xls,.xlsx) are programatically hard to deal with. However Excel will also read files in the.csv(Comma Separated Value) format, which is just a plain text file and therefore much easier to deal with.The Python
csvlibrary is dead easy to use – see the documentation for thecsvmodule.