Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Last active December 28, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelrevision/7453831 to your computer and use it in GitHub Desktop.
Save pixelrevision/7453831 to your computer and use it in GitHub Desktop.
Creates html tables from an excel doc
#!/usr/bin/python
import os
import shutil
import xlrd
import argparse
import xml.etree.ElementTree as elementTree
import xml.dom.minidom as minidom
def parseExcelDoc(xlsFile, outputFile, useTableHeader):
print xlsFile
table = xlrd.open_workbook(xlsFile)
sheetNames = table.sheet_names()
root = elementTree.Element("html")
head = elementTree.SubElement(root, "head")
title = elementTree.SubElement(head, "title")
title.text = "Converted tables"
body = elementTree.SubElement(root, "body")
for sn in sheetNames:
worksheet = table.sheet_by_name(sn)
table = elementTree.SubElement(body, "table")
table.set("class", sn.lower() )
if useTableHeader:
tableHead = elementTree.SubElement(table, "thead")
tableBody = elementTree.SubElement(table, "tbody")
numRows = worksheet.nrows
numCols = worksheet.ncols
currRow = 0
currentNode = tableBody
while currRow < numRows:
if useTableHeader == True and currRow == 0:
currentNode = tableHead
else:
currentNode = tableBody
row = worksheet.row(currRow)
currCol = 0
tableRow = elementTree.SubElement(currentNode, "tr")
tableCell = None
while currCol < numCols:
cellType = worksheet.cell_type(currRow, currCol)
cellValue = worksheet.cell_value(currRow, currCol)
if useTableHeader == True and currRow == 0:
tableCell = elementTree.SubElement(tableRow, "th")
else:
tableCell = elementTree.SubElement(tableRow, "td")
tableCell.text = str(cellValue)
currCol += 1
currRow += 1
tree = elementTree.ElementTree(root)
tree.write(outputFile)
xmlOutput = minidom.parse(outputFile)
formatted = xmlOutput.toprettyxml()
handle = open(outputFile, "w")
handle.write(formatted)
handle.close()
def start():
parser = argparse.ArgumentParser(description="Creates an html table from an excel file.")
parser.add_argument("-i", required=True, type=str, help="The excel file to parse.")
parser.add_argument("-o", required=True, type=str, help="The file to put the output in.")
parser.add_argument("-t", action='store_true', help="If the first row should be a table head or not.")
args = parser.parse_args()
xlsFile = os.path.abspath(args.i)
outputFile = None
if(args.o != None):
outputFile = os.path.abspath(args.o)
useTableHeader = args.t
parseExcelDoc(xlsFile, outputFile, useTableHeader)
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment