from xlsx2html import xlsx2html
html = xlsx2html("input.xlsx", "outfile.html")
如果源xlsx中包含中文内容,可能会存在乱码
让我们转到函数xlsx2html()的定义,找到HTML生成函数render_data_to_html()
def xlsx2html(
filepath,
output=None,
locale="en",
sheet=None,
parse_formula=False,
append_headers=(lambda dumb1, dumb2: True),
append_lineno=(lambda dumb1, dumb2: True),
default_cell_border="none",
):
wb = openpyxl.load_workbook(filepath, data_only=True)
ws = get_sheet(wb, sheet)
fs = None
if parse_formula:
fb = openpyxl.load_workbook(filepath, data_only=False)
fs = get_sheet(fb, sheet)
data = worksheet_to_data(
ws, locale=locale, fs=fs, default_cell_border=default_cell_border
)
#HTML生成函数
html = render_data_to_html(data, append_headers, append_lineno)
if not output:
output = io.StringIO()
if isinstance(output, str):
output = open(output, "w")
output.write(html)
output.flush()
return output
转到render_data_to_html(),修改模板中的编码
def render_data_to_html(data, append_headers, append_lineno):
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="GB2312">
<!--meta charset="UTF-8"修改编码格式-->
<title>Title</title>
</head>
<body>
%s
</body>
</html>
"""
return html % render_table(data, append_headers, append_lineno)