1

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

amlane86
  • 668
  • 4
  • 15
  • 24

3 Answers3

2

I see two parts to this problem:

  1. Extracting the data from the HTML table.
  2. Exporting the data in a format understandable by Excel.

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 csv library is dead easy to use - see the documentation for the csv module.

Community
  • 1
  • 1
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
  • Thanks for the advice. I am going to look into finding the source that created the table and see if I can't extract the data that way. I have been instructed to create an .xml file... and ahve done that successfully with static data... but not dynamically written data. – amlane86 Feb 20 '12 at 15:39
  • If the data's coming from a database of some kind, that's dead easy. Support for most major databases is already baked into Python (or any other programming language you might care to use.) Can you tell use a bit more about where the data is coming from? – Li-aung Yip Feb 21 '12 at 04:01
0

In general:

Link to a page that sends out an EXCEL Mime header. Then just spit out an HTML page with a table in it. I'm not sure how this works on Python.

This may help:

Do all browsers support excel respond?

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

I am not certain I agree with the approach 100%, unless you are screen scrapping from a server you have no control over.

If you do have control over the server providing the data, I would pass the variables that generated the table to begin with back to python and conduct another database query specific to this request.

From there, generating an excel file is easy, by using python-excel.

I'm also not sure what you mean by "export"? Is this something to be downloaded by the client, or something emailed when the user presses go, or something else?

Which webframework are you working in (if any)?

James R
  • 4,571
  • 3
  • 30
  • 45
  • 1
    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. – amlane86 Feb 17 '12 at 17:25