import os
from xlrd import open_workbook,formatting
def Excel2Html(filename, with_text=True):
"""
该函数把.xls的表格文件转化为html字符串
参数:
with_text为True:输出表格结构和文字信息
with_text为False:只输出表格结构信息
"""
filepath = filename
# wb = open_workbook(filepath,formatting_info=True)
wb = open_workbook(filepath,formatting_info=False)
sheet = wb.sheet_by_index(0)
if with_text:
html = '<table class="previewtable" border="1" cellpadding="0" cellspacing="0">'
else:
html = '<table>'
mergedcells={}
mergedsapn={}
mergedcellvalue={}
for crange in sheet.merged_cells:
rlo, rhi, clo, chi = crange
for rowx in range(rlo, rhi):
for colx in range(clo, chi):
mergedcells[(rowx,colx)]=False
value = str(sheet.cell_value(rowx,colx))
if value.strip() != '':
mergedcellvalue[(rlo,clo)]=value
mergedcells[(rlo,clo)]=True
mergedsapn[(rlo,clo)]=(rhi-rlo, chi-clo)
mergedsapn[(rlo,clo)]=(rhi-rlo, chi-clo)
for row in range(sheet.nrows):
html=html+'<tr>'
for col in range(sheet.ncols):
if (row,col) in mergedcells:
if mergedcells[(row,col)]==True:
rspan,cspan = mergedsapn[(row,col)]
value = ''
if (row,col) in mergedcellvalue:
value = mergedcellvalue[(row,col)]
if with_text:
html=html+'<td rowspan=%s colspan=%s>%s</td>' % (rspan, cspan, value)
else:
html=html+'<td rowspan=%s colspan=%s></td>' % (rspan, cspan)
else:
value =sheet.cell_value(row,col)
if with_text:
html=html+'<td>' + str(value) + '</td>'
else:
html=html+'<td>' + '</td>'
html=html+'</tr>'
html=html+'</table>'
return html.replace(" rowspan=1", "").replace(" colspan=1", "")
if __name__ == "__main__":
import codecs
html_string = Excel2Html("test.xlsx", True)
print(html_string)
with codecs.open("dada.html", "w") as html_file:
html_file.write(html_string)
如果有些表格格式不支持,请参考:.xlsx .xls