excel2html 把表格转化为html

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值