By Vasudev Ram
I had come across openpyxl, a library by Eric Gazoni, for reading and writing Microsoft Excel XLSX files (Open Office XML), a while ago.
So today I wrote a demo program that reads the data from an XLSX file using openpyxl and writes that data to HTML as a table. Here is a screenshot of the sample XLSX file used, fruits.xlsx (click image to enlarge):
Here is the program, XLSXtoHTMLdemo.py:
# XLSXtoHTMLdemo.py # Program to convert the data from an XLSX file to HTML. # Uses the openpyxl library. # Author: Vasudev Ram - http://www.dancingbison.com import openpyxl from openpyxl import load_workbook workbook = load_workbook('fruits.xlsx') worksheet = workbook.get_active_sheet() html_data = """ <html> <head> <title> XLSX to HTML demo <title> <head> <body> <h3> XLSX to HTML demo <h3> <table> """ ws_range = worksheet.range('A1:H13') for row in ws_range: html_data += "<tr> for cell in row: if cell.value is None: html_data += "<td> + ' ' + "<td> else: html_data += "<td> + str(cell.value) + "<td> html_data += "<tr> html_data += "<table>lt;body>lt;html> with open("fruits.html", "w") as html_fil: html_fil.write(html_data) # EOFYou can run the program with:
python XLSXtoHTMLdemo.pyThen the program's HTML output will be in the file fruits.html, a screenshot of which is below (click to enlarge):
- Vasudev Ram - Python, C, Linux, databases, open source - training and consulting.
Read all Python posts on my blog.