You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python如何保留单元格样式将Excel文件转为HTML格式?

如何用Python在RedHat Linux上将带样式的Excel(.xls/.xlsx)导出为HTML

首先,你提到的pandas.DataFrame.to_html()确实不会保留Excel中的单元格样式——它只专注于导出表格数据,完全忽略字体、颜色、对齐等格式信息,这就是你丢失样式的原因。下面给你两种在RedHat Linux环境下可行的解决方案:

方案1:使用现成库xlsx2html(简单快捷)

xlsx2html是一个专门用于将Excel文件转换为HTML的工具,支持保留大部分单元格样式(背景色、字体样式、边框、对齐方式等),同时兼容.xlsx.xls格式(处理xls需要依赖旧版本的xlrd)。

步骤:

  1. 安装依赖:
    pip install xlsx2html xlrd==1.2.0  # xlrd 1.2.0用于支持.xls格式
    
  2. 使用Python代码转换:
    import xlsx2html
    
    # 转换xlsx文件
    xlsx2html.convert(r"C:/Users/Anoop/sample.xlsx", r"C:/Users/Anoop/sample_xlsx.html")
    
    # 转换xls文件
    xlsx2html.convert(r"C:/Users/Anoop/sample.xls", r"C:/Users/Anoop/sample_xls.html")
    

这个方法无需手动处理样式,库会自动解析Excel中的格式并生成对应的HTML和CSS,适合快速实现需求。

方案2:手动解析样式并生成HTML(自定义程度高)

如果你需要更精细地控制HTML输出(比如调整样式优先级、添加自定义布局),可以使用openpyxl(处理.xlsx)和xlrd(处理.xls)读取Excel的样式信息,手动构建带有样式的HTML表格。

示例:处理.xlsx文件(使用openpyxl)

  1. 安装依赖:
    pip install openpyxl
    
  2. 代码示例:
    from openpyxl import load_workbook
    
    def excel_xlsx_to_html_with_style(input_path, output_path):
        wb = load_workbook(input_path)
        ws = wb.active  # 获取第一个工作表
        html_content = ['<table style="border-collapse: collapse;">']
    
        # 遍历每一行
        for row in ws.iter_rows():
            html_content.append('<tr>')
            for cell in row:
                style = []
                # 背景色
                if cell.fill.fgColor.rgb:
                    style.append(f'background-color: #{cell.fill.fgColor.rgb[2:]};')
                # 字体颜色
                if cell.font.color.rgb:
                    style.append(f'color: #{cell.font.color.rgb[2:]};')
                # 字体加粗
                if cell.font.bold:
                    style.append('font-weight: bold;')
                # 单元格对齐
                if cell.alignment.horizontal:
                    style.append(f'text-align: {cell.alignment.horizontal};')
                # 边框样式
                style.append('border: 1px solid #ccc; padding: 4px;')
    
                # 拼接单元格HTML
                cell_style = ' '.join(style)
                html_content.append(f'<td style="{cell_style}">{cell.value if cell.value is not None else ""}</td>')
            html_content.append('</tr>')
        
        html_content.append('</table>')
        
        # 写入HTML文件
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write('\n'.join(html_content))
    
    # 调用函数转换文件
    excel_xlsx_to_html_with_style(r"C:/Users/Anoop/sample.xlsx", r"C:/Users/Anoop/sample_styled.html")
    

处理.xls文件(使用xlrd)

由于xlrd 2.0+版本不再支持.xls格式,需要安装1.2.0版本:

pip install xlrd==1.2.0

xlrd读取样式的逻辑相对复杂,需要解析颜色索引和格式属性,这里给一个简化示例:

import xlrd

def excel_xls_to_html_with_style(input_path, output_path):
    wb = xlrd.open_workbook(input_path, formatting_info=True)
    ws = wb.sheet_by_index(0)
    html_content = ['<table style="border-collapse: collapse;">']

    # 获取Excel内置颜色映射
    color_map = wb.colour_map

    # 遍历每一行
    for row_idx in range(ws.nrows):
        html_content.append('<tr>')
        for col_idx in range(ws.ncols):
            cell = ws.cell(row_idx, col_idx)
            xf = wb.xf_list[cell.xf_index]
            style = []

            # 背景色
            bg_color_idx = xf.background.pattern_colour_index
            if bg_color_idx in color_map:
                bg_color = color_map[bg_color_idx]
                style.append(f'background-color: #{bg_color:06x};')
            # 字体颜色
            font = wb.font_list[xf.font_index]
            font_color_idx = font.colour_index
            if font_color_idx in color_map:
                font_color = color_map[font_color_idx]
                style.append(f'color: #{font_color:06x};')
            # 字体加粗
            if font.bold:
                style.append('font-weight: bold;')
            # 边框与内边距
            style.append('border: 1px solid #ccc; padding: 4px;')

            cell_style = ' '.join(style)
            html_content.append(f'<td style="{cell_style}">{cell.value if cell.value is not None else ""}</td>')
        html_content.append('</tr>')
    
    html_content.append('</table>')
    
    with open(output_path, 'w', encoding='utf-8') as f:
        f.write('\n'.join(html_content))

# 调用函数转换文件
excel_xls_to_html_with_style(r"C:/Users/Anoop/sample.xls", r"C:/Users/Anoop/sample_xls_styled.html")

注意事项

  • 对于复杂的Excel样式(比如合并单元格、条件格式、图表),xlsx2html的兼容性会更好;手动解析的方案需要额外处理这些特殊场景。
  • 在RedHat Linux环境下,若遇到权限问题,安装依赖时可以加上--user参数:pip install --user xlsx2html xlrd==1.2.0

内容的提问来源于stack exchange,提问作者anoopkiid

火山引擎 最新活动