当前位置:   article > 正文

Python Openpyxl xlsx转html_python excel转化html

python excel转化html

目录​​​​​​​

Excel转Html

示例

函数

Openpyxl知识点

其他Excel解析库问题


Excel转Html

示例

函数

  1. import openpyxl
  2. from openpyxl.styles import Border
  3. from openpyxl.styles import Font
  4. from openpyxl.styles import Side
  5. from openpyxl.styles import Alignment
  6. from openpyxl.styles import PatternFill
  7. from openpyxl.cell import MergedCell
  8. # 模式:adaptive / normal
  9. def transform_xlsx_to_html(m_sheet, m_mode):
  10. # sheet.min_row的值在后续数据获取过程中会改变,原因不明
  11. m_min_row = m_sheet.min_row
  12. m_max_row = m_sheet.max_row
  13. m_min_col = m_sheet.min_column
  14. m_max_col = m_sheet.max_column
  15. if m_min_row == m_max_row == m_min_col == m_max_col and not m_sheet.cell(m_min_row, m_min_col).value:
  16. return ''
  17. m_merged_cell_dict = {} # 用于储存 所有合并单元格的左上单元格对象
  18. m_width_dict = {} # 用于储存 所有列的列宽,px
  19. m_height_dict = {} # 用于储存 所有列的行高,px
  20. # 查询行高
  21. for m_i in range(m_min_row, m_max_row + 1):
  22. m_height = 13.5
  23. if m_sheet.row_dimensions[m_i].height:
  24. m_height = m_sheet.row_dimensions[m_i].height
  25. m_height_dict[str(m_i)] = m_height
  26. # 查询列宽
  27. for m_i in range(m_min_col, m_max_col + 1):
  28. m_col_name = openpyxl.utils.get_column_letter(m_i)
  29. m_width = m_sheet.column_dimensions[m_col_name].width
  30. if m_mode == 'normal':
  31. m_width = m_sheet.column_dimensions[m_col_name].width * 6
  32. m_width_dict[m_col_name] = m_width
  33. # 找出所有合并区域的行高,列宽,向右合并距离,向下合并距离
  34. for m_merged_range in m_sheet.merged_cells.ranges:
  35. m_width = 0 # 定义列宽
  36. m_height = 0 # 定义行高
  37. for m_i in range(m_merged_range.min_row, m_merged_range.max_row + 1):
  38. m_height += m_height_dict[str(m_i)]
  39. for m_i in range(m_merged_range.min_col, m_merged_range.max_col + 1):
  40. m_col_name = openpyxl.utils.get_column_letter(m_i)
  41. m_width += m_width_dict[m_col_name]
  42. m_cell = m_sheet.cell(row=m_merged_range.min_row, column=m_merged_range.min_col) # 选择合并区域左上单元格
  43. m_colspan = m_merged_range.max_col - m_merged_range.min_col + 1 # 向右合并长度
  44. m_rowspan = m_merged_range.max_row - m_merged_range.min_row + 1 # 向下合并长度
  45. m_merged_cell_dict[m_cell] = (m_height, m_width, m_colspan, m_rowspan)
  46. # 开始写入数据到table标签
  47. m_html = '''<table style="border: 1px solid #A9A9A9">'''
  48. for m_i in range(m_min_row, m_max_row + 1):
  49. tr = '''<tr>'''
  50. for m_j in range(m_min_col, m_max_col + 1):
  51. m_cell = m_sheet.cell(m_i, m_j)
  52. m_col_name = openpyxl.utils.get_column_letter(m_j)
  53. # html 字体大小
  54. m_font_size = str(int(m_cell.font.size) + 3) if m_cell.font.size else 10
  55. # html 字体加粗
  56. m_font_weight = '700' if m_cell.font.b else '400'
  57. # html 边框格式
  58. m_border = 'border: 1px solid #A9A9A9' if m_cell.value or m_cell.value == 0 else 'border: 0'
  59. # 水平位置
  60. m_align_horizontal = 'text-align: {}'.format(m_cell.alignment.horizontal) if m_cell.alignment.horizontal else ''
  61. # 垂直位置
  62. m_align_vertical = 'vertical-align: {}'.format(m_cell.alignment.vertical) if m_cell.alignment.vertical else ''
  63. # 字体颜色 颜色未设置时有时会存在 Values must be of type <class 'str'> 输出
  64. if m_cell.font.color and 'str' not in str(m_cell.font.color.rgb):
  65. m_font_color = 'color: #{}'.format(m_cell.font.color.rgb[2:])
  66. else:
  67. m_font_color = 'color: #000000'
  68. # 背景颜色 颜色未设置时有时会存在 Values must be of type <class 'str'> 输出
  69. if m_cell.fill.fgColor and 'str' not in str(m_cell.fill.fgColor.rgb) and m_cell.fill.fgColor.rgb[2:] != '000000':
  70. m_fill_color = 'background-color: #{}'.format(m_cell.fill.fgColor.rgb[2:])
  71. else:
  72. m_fill_color = 'background-color: #FFFFFF'
  73. # 单元格格式
  74. style = f'''{m_fill_color}; {m_border}; {m_font_color};
  75. font-style: normal;
  76. font-family: 微软雅黑;
  77. font-size: {m_font_size}px;
  78. font-weight: {m_font_weight};
  79. {m_align_horizontal}; {m_align_vertical}; '''
  80. if m_mode == 'normal':
  81. if m_cell in m_merged_cell_dict.keys():
  82. style += f'''height: {m_merged_cell_dict[m_cell][0]}px; width: {m_merged_cell_dict[m_cell][1]}px; '''
  83. else:
  84. style += f'''height: {m_height_dict[str(m_i)]}px; width: {m_width_dict[m_col_name]}px; '''
  85. else:
  86. style += f'''word-break: keep-all; word-wrap:break-word; white-space: nowrap'''
  87. text = m_cell.value if m_cell.value or m_cell.value == 0 else ''
  88. # 文本处理:小数转为百分比展示
  89. text = '{:.2f}%'.format(text * 100) if text and type(text) == float else text
  90. # 文本处理:字符串换行展示
  91. text = text.replace('\n', '<br/>') if text and type(text) == str else text
  92. td = ""
  93. if m_cell in m_merged_cell_dict.keys():
  94. td = f'''<td colspan="{m_merged_cell_dict[m_cell][2]}" rowspan="{m_merged_cell_dict[m_cell][3]}" style="{style}">{text}</td>'''
  95. elif not isinstance(m_cell, MergedCell):
  96. td = f'''<td style="{style}">{text}</td>'''
  97. tr = tr + td
  98. tr = tr + '''</tr>'''
  99. m_html += tr
  100. m_html += '''</table>'''
  101. return m_html

Openpyxl知识点

函数功能
workbook.sheetnames返回一个列表,记录所有Sheet页名称
workbook[sheetname]定位Sheet页
workbook.remove(sheetname)删除Sheet页
sheet.min_row
sheet.max_row
sheet.min_column
sheet.max_column

获取有效数据首行(有时会改变,原因不明)

获取有效数据末行

获取有效数据首列

获取有效数据末列

sheet.views.sheetView[0].showGridLines设置网格线

sheet.row_dimensions[row_index].height

sheet.column_dimensions[column_name].width

openpyxl.utils.get_column_letter(column_index)

获取、设置行高

获取、设置列宽

获取下标对应的列名(从1开始)

cell.value

cell.alignment

cell.number_format

获取、设置文本

获取、设置对齐

获取、设置数字格式

cell.border = Border()

cell.border.left.style

cell.border.top.style

cell.border.right.style

cell.border.bottom.style

设置边框格式

获取边框格式-左(不能直接获取)

获取边框格式-上(不能直接获取)

获取边框格式-右(不能直接获取)

获取边框格式-下(不能直接获取)

cell.font

cell.font.size
cell.font.b

cell.font.color.rgb

设置字体格式

获取字体信息-尺寸

获取字体信息-粗细

获取字体颜色(不能直接获取)

cell.fill

cell.fill.fgColor.rgb

设置填充颜色

获取填充颜色(不能直接获取)

cell.coordinate
cell.alignment.vertical
cell.alignment.horizontal

获取坐标信息

获取对齐信息-竖直对齐

获取对齐信息-水平对齐

sheet.merge_cells()合并单元格
isinstance(cell, MergedCell)判断是否为合并单元格
sheet.merged_cells.ranges
sheet.merged_cells.ranges[index].min_row
sheet.merged_cells.ranges[index]..max_row
sheet.merged_cells.ranges[index]..min_col
sheet.merged_cells.ranges[index]..max_col
获取合并单元格信息

 示例代码:

  1. import openpyxl
  2. from openpyxl.styles import Border
  3. from openpyxl.styles import Font
  4. from openpyxl.styles import Side
  5. from openpyxl.styles import Alignment
  6. from openpyxl.styles import PatternFill
  7. from openpyxl.cell import MergedCell
  8. # 打开Excel
  9. workbook = openpyxl.load_workbook(filename='D:\Project\test.xlsx')
  10. print(workbook.sheetnames)
  11. # 创建删除sheet
  12. workbook.create_sheet('test')
  13. workbook.remove('test')
  14. # 行数列数
  15. sheet = workbook.sheetnames[0]
  16. print(sheet.min_row)
  17. print(sheet.max_row)
  18. print(sheet.min_column)
  19. print(sheet.max_column)
  20. # 行高列宽
  21. print(sheet.row_dimensions[1].height )
  22. print(sheet.column_dimensions['A'].width)
  23. # 设置行高列宽
  24. sheet.row_dimensions[1].height = 10
  25. sheet.column_dimensions['A'].width = 10
  26. # 获取下标对应的列名(从1开始)
  27. column_name = openpyxl.utils.get_column_letter(1)
  28. # 设置不显示网格线
  29. sheet.views.sheetView[0].showGridLines = False
  30. # 合并单元格
  31. sheet.merge_cells(start_row=1, end_row=2, start_column=3, end_column=4)
  32. # 设置数字格式
  33. sheet.cell(row=2, column=2, value=100).number_format = '0'
  34. sheet.cell(row=3, column=3, value=100).number_format = '0.00%'
  35. # 设置单元格
  36. sheet.cell(row=1, column=1, value='test')
  37. # 设置填充颜色
  38. fill = PatternFill('solid', fgColor='123456')
  39. # 设置字体格式
  40. font = Font(u'微软雅黑', size=16, bold=True, italic=False, strike=False, color='000000')
  41. # 设置文本对齐
  42. alignment = Alignment(wrap_text=True, horizontal='center', vertical='center')
  43. # 方式一
  44. sheet.cell(row=1, column=1, fill=fill, font=font, alignment=alignment, value='test')
  45. # 方式二
  46. sheet.cell(row=1, column=1).value = 'test'
  47. sheet.cell(row=1, column=1).fill = fill
  48. sheet.cell(row=1, column=1).font = font
  49. sheet.cell(row=1, column=1).alignment = alignment
  50. # 设置边框 方式一 合并单元格时右侧边框设置会不成功
  51. border = Border()
  52. border.left = Side(border_style='double', color='000000')
  53. border.top = Side(border_style='double', color='000000')
  54. border.right = Side(border_style='double', color='000000')
  55. border.bottom = Side(border_style='double', color='000000')
  56. # 设置边框 方式二 合并单元格时右侧边框设置会不成功
  57. border = Border(left=Side(border_style='double', color='000000'),
  58. top=Side(border_style='double', color='000000'),
  59. right=Side(border_style='double', color='000000'),
  60. bottom=Side(border_style='double', color='000000'))
  61. # 使用下标索引时从0开始
  62. cell = sheet[0][0]
  63. cell = sheet.cell(row=1, column=1)
  64. # 获取单元格信息
  65. print(cell.value)
  66. print(cell.coordinate)
  67. # 获取字体信息
  68. print(cell.font.size)
  69. print(cell.font.b)
  70. # 获取对齐信息
  71. print(cell.alignment.vertical)
  72. print(cell.alignment.horizontal)
  73. # 判断是否为合并单元格
  74. print(isinstance(cell, MergedCell))
  75. # 获取边框信息 未初始化时不可直接获取
  76. left = cell.border.left.style if cell.border.left else ''
  77. top = cell.border.top.style if cell.border.top else ''
  78. right = cell.border.right.style if cell.border.right else ''
  79. bottom = cell.border.bottom.style if cell.border.bottom else ''
  80. # 获取颜色信息 未初始化时不可直接获取
  81. font_color = cell.font.color.rgb if 'str' not in str(cell.font.color.rgb) else ''
  82. fill_color = cell.fill.fgColor.rgb if 'str' not in str(cell.fill.fgColor.rgb) else ''
  83. # 获取合并单元格信息
  84. merged_list = sheet.merged_cells.ranges
  85. merged_cell = sheet.merged_cells.ranges[0]
  86. print(merged_cell.min_row)
  87. print(merged_cell.max_row)
  88. print(merged_cell.min_col)
  89. print(merged_cell.max_col)

其他Excel解析库问题

库名说明
pandas支持数据处理,不支持格式获取
xlsx2html支持简单表格,不支持合并单元格
xlwings支持Windows和MacOS系统,Linux系统需要使用xlwings pro

参考资料:

python将excel转html的table标签(含合并单元格)_python xlsx转html_weixin_50702169的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/249969
推荐阅读
相关标签
  

闽ICP备14008679号