这个你看看,跟着改一下,你那个图片要设置路径,不然出不来的,还有表格的名称。
[Python] 纯文本查看 复制代码 import pandas as pd
# 读取 Excel 文件
file_path = 'path_to_your_excel_file.xlsx' # 替换为你的 Excel 文件路径
df = pd.read_excel(file_path)
# HTML 模板
html_template = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
h3 {
text-align: center
}
table {
border-collapse: collapse;
font-size: 15px;
width: 100%
}
td {
border: 1px solid #dfdfdf;
padding: 5px
}
table td:first-child {
width: 40%;
background-color: #eee;
margin: 10px;
padding: 15px;
}
</style>
</head>
<body>
<table>
<tr>
<td>船舶现识别牌号</td>
<td>{ship_current_id}</td>
</tr>
<tr>
<td>纳管船舶识别牌号</td>
<td>{ship_management_id}</td>
</tr>
<tr>
<td>船东姓名</td>
<td>{owner_name}</td>
</tr>
<tr>
<td>总功率(马力)</td>
<td>{power}</td>
</tr>
<tr>
<td>船体材质</td>
<td>{material}</td>
</tr>
<tr>
<td>船舶总长(米)</td>
<td>{length}</td>
</tr>
<tr>
<td>日常停泊地点</td>
<td>{parking_spot}</td>
</tr>
<tr>
<td>作业区域</td>
<td>{work_area}</td>
</tr>
</table><br>
<center><p>船舶照片</p></center>
<img src="{photo}" width="100%">
</body>
<script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?96f677bd2194ff927f22a70eba39c8bc"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>
</html>
"""
# 生成 HTML 文件
for index, row in df.iterrows():
html_content = html_template.format(
ship_current_id=row['船舶现识别牌号'],
ship_management_id=row['纳管船舶识别牌号'],
owner_name=row['船东姓名'],
power=row['总功率(马力)'],
material=row['船体材质'],
length=row['船舶总长(米)'],
parking_spot=row['日常停泊地点'],
work_area=row['作业区域'],
photo=row['船舶照片']
)
file_name = f"{row['序号']}.html"
with open(file_name, "w", encoding="utf-8") as file:
file.write(html_content)
print("HTML files generated successfully.")
|