Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 2, 2023 03:08
Show Gist options
  • Save code-boxx/449b31883b552edded7909f97ea4b319 to your computer and use it in GitHub Desktop.
Save code-boxx/449b31883b552edded7909f97ea4b319 to your computer and use it in GitHub Desktop.
Python Flask Display Excel File In HTML Table

PYTHON DISPLAY EXCEL IN HTML TABLE

https://code-boxx.com/excel-html-table-python-flask/

NOTES

  1. GIST does not allow Excel files. Convert S1_dummy.csv to S1_dummy.xlsx on your own, OR generate your own dummy Excel file.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a templates folder, move S3_excel_table.html inside.
    • Create a virtual environment - virtualenv venv.
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Install Flask + PYExcel - pip install openpyxl flask
    • Run python S2_server.py to start the server.
  3. Access http://localhost in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jo Doe jo@doe.com 465785
Joa Doe joa@doe.com 123456
Job Doe job@doe.com 234567
Joe Doe joe@doe.com 345678
Jog Doe jog@doe.com 578456
Joh Doe joh@doe.com 378945
Joi Doe joi@doe.com 456789
Jon Doe jon@doe.com 987654
Jor Doe jor@doe.com 754642
Joy Doe joy@doe.com 124578
# (A) INIT
# (A1) LOAD MODULES
from flask import Flask, render_template, request, make_response
from openpyxl import load_workbook
# (A2) FLASK SETTINGS + INIT
HOST_NAME = "localhost"
HOST_PORT = 80
app = Flask(__name__)
# app.debug = True
# (B) DEMO - READ EXCEL & GENERATE HTML TABLE
@app.route("/")
def index():
# (B1) OPEN EXCEL FILE + WORKSHEET
book = load_workbook("S1_dummy.xlsx")
sheet = book.active
# (B2) PASS INTO HTML TEMPLATE
return render_template("S3_excel_table.html", sheet=sheet)
# (C) START
if __name__ == "__main__":
app.run(HOST_NAME, HOST_PORT)
<!DOCTYPE html>
<html>
<head>
<title>Excel To HTML Table</title>
<style>
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
#demo { border-collapse: collapse; }
#demo tr:nth-child(odd) { background: #f2f2f2; }
#demo td { padding: 10px; }
</style>
</head>
<body>
<table id="demo">
{% for row in range(1, sheet.max_row + 1) %}
<tr>
{% for col in range(1, sheet.max_column + 1): %}
<td>{{ sheet.cell(row, col).value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
md templates
move S3_excel_table.html templates
virtualenv venv
call venv\Scripts\activate
pip install openpyxl flask
python S2_server.py
mkdir -m 777 templates
mv ./S3_excel_table.html ./templates
virtualenv venv
source "venv/bin/activate"
pip install openpyxl flask
python S2_server.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment