Generating an HTML Table Using Python 3

If you ever need to generate an HTML table from file data, here is a basic Python 3 script that should do the trick. If you’d rather use Shell Script, check out the Generating an HTML Table Using Shell Script tutorial.

#!/usr/bin/python3

# Run as: create-html-table.py {input-file-name}
# The script requires 1 argument: the input file name.
# It expects a comma-separated input file to parse into an html table,
# and assumes that the column headers are located in the first row.

import sys

filein = open(sys.argv[1], "r")
fileout = open("html-table.html", "w")
data = filein.readlines()

table = "<table>\n"

# Create the table's column headers
header = data[0].split(",")
table += "  <tr>\n"
for column in header:
    table += "    <th>{0}</th>\n".format(column.strip())
table += "  </tr>\n"

# Create the table's row data
for line in data[1:]:
    row = line.split(",")
    table += "  <tr>\n"
    for column in row:
        table += "    <td>{0}</td>\n".format(column.strip())
    table += "  </tr>\n"

table += "</table>"

fileout.writelines(table)
fileout.close()
filein.close()

Input File

The input file should contain data in a comma-delimited format with the column headers located in the first row. For example,

No.,Package,Priority,Is a Dependency?
1,adduser,important,yes
2,apt,important,yes
3,apt-utils,important,no
4,base-files,required,yes
5,base-passwd,required,yes
6,bash,required,no
7,bsdmainutils,important,no
8,bsdutils,required,yes
9,coreutils,required,yes
10,cpio,important,yes
11,cron,important,yes
12,dash,required,yes
13,debconf,required,yes
14,debconf-i18n,important,no
15,debian-archive-keyring,important,yes
16,debianutils,required,yes
17,diffutils,required,no
18,dmidecode,important,no
19,dmsetup,optional,yes
20,dpkg,required,yes

Output File

The output file will be created in the current bash/PowerShell directory, and will be named “html-table.html”. The file will contain the input file’s data formatted into a HTML table structure. For example,

<table>
  <tr>
    <th>No.</th>
    <th>Package</th>
    <th>Priority</th>
    <th>Is a Dependency?</th>
  </tr>
  <tr>
    <td>1</td>
    <td>adduser</td>
    <td>important</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>2</td>
    <td>apt</td>
    <td>important</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>3</td>
    <td>apt-utils</td>
    <td>important</td>
    <td>no</td>
  </tr>
  <tr>
    <td>4</td>
    <td>base-files</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>5</td>
    <td>base-passwd</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>6</td>
    <td>bash</td>
    <td>required</td>
    <td>no</td>
  </tr>
  <tr>
    <td>7</td>
    <td>bsdmainutils</td>
    <td>important</td>
    <td>no</td>
  </tr>
  <tr>
    <td>8</td>
    <td>bsdutils</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>9</td>
    <td>coreutils</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>10</td>
    <td>cpio</td>
    <td>important</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>11</td>
    <td>cron</td>
    <td>important</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>12</td>
    <td>dash</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>13</td>
    <td>debconf</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>14</td>
    <td>debconf-i18n</td>
    <td>important</td>
    <td>no</td>
  </tr>
  <tr>
    <td>15</td>
    <td>debian-archive-keyring</td>
    <td>important</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>16</td>
    <td>debianutils</td>
    <td>required</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>17</td>
    <td>diffutils</td>
    <td>required</td>
    <td>no</td>
  </tr>
  <tr>
    <td>18</td>
    <td>dmidecode</td>
    <td>important</td>
    <td>no</td>
  </tr>
  <tr>
    <td>19</td>
    <td>dmsetup</td>
    <td>optional</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>20</td>
    <td>dpkg</td>
    <td>required</td>
    <td>yes</td>
  </tr>
</table>

Output File Rendered in Website’s CSS

Here is the output file’s HTML table rendered with the CSS formatting used by this website.

No. Package Priority Is a Dependency?
1 adduser important yes
2 apt important yes
3 apt-utils important no
4 base-files required yes
5 base-passwd required yes
6 bash required no
7 bsdmainutils important no
8 bsdutils required yes
9 coreutils required yes
10 cpio important yes
11 cron important yes
12 dash required yes
13 debconf required yes
14 debconf-i18n important no
15 debian-archive-keyring important yes
16 debianutils required yes
17 diffutils required no
18 dmidecode important no
19 dmsetup optional yes
20 dpkg required yes