保留Excel单元格格式转换为HTML表格的方法及Python实现方案
可行解决方案
推荐使用openpyxl读取Excel内容与样式,自定义生成带内联样式的HTML表格,可精准控制格式保留,同时支持指定工作表、指定单元格范围的需求。
依赖安装
执行以下命令安装所需库:pip install openpyxl
核心实现逻辑
- 用openpyxl加载Excel文件,选中指定工作表
- 遍历目标单元格范围,逐个读取单元格的内容、字体、填充、边框、对齐、数字格式等属性
- 将Excel样式属性转换为CSS内联样式,拼接成完整的HTML table结构
- 最终输出完整的静态HTML文件
示例代码
from openpyxl import load_workbook from openpyxl.styles import PatternFill, Border, Side def argb_to_hex(argb_str): # 转换openpyxl的ARGB颜色为HTML十六进制RGB if argb_str and len(argb_str) == 8: return f"#{argb_str[2:]}" return "transparent" def excel_to_html(excel_path, sheet_name, start_row, end_row, start_col, end_col, output_html_path): wb = load_workbook(excel_path, data_only=True) ws = wb[sheet_name] html_parts = [ "<!DOCTYPE html>", "<html>", "<head><meta charset='UTF-8'><title>Excel转HTML结果</title></head>", "<body>", "<table style='border-collapse: collapse;'>" ] for row in range(start_row, end_row + 1): html_parts.append("<tr>") for col in range(start_col, end_col + 1): cell = ws.cell(row=row, column=col) # 处理样式 style = [] # 背景色 if cell.fill.patternType == 'solid': bg_color = argb_to_hex(cell.fill.fgColor.rgb) style.append(f"background-color: {bg_color};") # 字体 if cell.font: if cell.font.color and cell.font.color.rgb: font_color = argb_to_hex(cell.font.color.rgb) style.append(f"color: {font_color};") if cell.font.bold: style.append("font-weight: bold;") if cell.font.italic: style.append("font-style: italic;") style.append(f"font-size: {cell.font.sz}px;") # 边框 if cell.border: def get_border_style(side: Side): if not side or side.style is None: return "none" border_style_map = { 'thin': '1px solid', 'thick': '2px solid', 'dashed': '1px dashed', 'dotted': '1px dotted' } color = argb_to_hex(side.color.rgb) if side.color else "#000000" return f"{border_style_map.get(side.style, '1px solid')} {color}" style.append(f"border-top: {get_border_style(cell.border.top)};") style.append(f"border-bottom: {get_border_style(cell.border.bottom)};") style.append(f"border-left: {get_border_style(cell.border.left)};") style.append(f"border-right: {get_border_style(cell.border.right)};") # 对齐 if cell.alignment: if cell.alignment.horizontal: style.append(f"text-align: {cell.alignment.horizontal};") if cell.alignment.vertical: style.append(f"vertical-align: {cell.alignment.vertical};") if cell.alignment.wrapText: style.append("white-space: pre-wrap;") # 单元格内容 cell_value = cell.value if cell.value is not None else "" style_str = ' '.join(style) html_parts.append(f"<td style='{style_str}'>{cell_value}</td>") html_parts.append("</tr>") html_parts.extend([ "</table>", "</body>", "</html>" ]) with open(output_html_path, 'w', encoding='utf-8') as f: f.write('\n'.join(html_parts)) # 调用示例 if __name__ == "__main__": excel_to_html( excel_path="测试文件.xlsx", sheet_name="目标工作表", start_row=1, end_row=30, start_col=1, end_col=8, output_html_path="输出结果.html" )
扩展说明
如果需要处理合并单元格、数字格式、单元格宽度高度等属性,可在上述代码基础上扩展对应的样式读取逻辑,所有Excel单元格的样式属性都可以通过openpyxl的API获取后转换为对应的CSS规则。
内容的提问来源于stack exchange,提问作者Mat
相关产品推荐
相关产品推荐

