正如您在下面的代码中看到的,我正在生成一个包含4列的HTML表。第一列和第二列有1行,第3和第4列有5行。
我尝试过使用rowspan但是无法使用它并且它会产生奇怪的输出。现在我使用嵌套在主table
中的table
元素,但我不想使用它,我想使用rowspan
from dominate import tags as tags# import time, subprocess, os, datetime, zipfile, shutil
doc = dominate.document(title="whatever")
with doc:
tags.style("body{font-family:Helvetica}")
tags.style("h1{font-size:x-large}")
tags.style("h2{font-size:large}")
tags.style("table{border-collapse:collapse}")
tags.style("th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}")
tags.style("td{font-size:small;text-align:center;border:1px solid gray;padding:4px}")
with tags.table():
with tags.thead():
tags.th("Test")
tags.th("Pass%")
tags.th("random")
tags.th("Bugs / Notes")
with tags.tbody():
with tags.tr():
tags.td("random")
tags.td("random", "%", style="font-size:small;text-align:center;border:1px solid gray;padding:4px;background-color:#66FF66")
with tags.td():
with tags.table(style = ""):
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.td():
with tags.table():
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr():
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
HTML_TMP = "/tmp/demo.html"
MAIL_TMP = "/tmp/demo.txt"
# save it to a file
with open(HTML_TMP, "w") as f:
f.write(str(doc))
你能告诉我怎么做。
答案 0 :(得分:1)
我认为你的问题与HTML结构有关。您想要从主表中删除嵌入表,只需在第一行中为前两个单元格提供属性rowspan="5"
。下面的代码会对您的代码进行最少的其他更改。
import dominate
from dominate import tags as tags# import time, subprocess, os, datetime, zipfile, shutil
doc = dominate.document(title="whatever")
with doc:
tags.style("body{font-family:Helvetica}")
tags.style("h1{font-size:x-large}")
tags.style("h2{font-size:large}")
tags.style("table{border-collapse:collapse}")
tags.style("th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}")
tags.style("td{font-size:small;text-align:center;border:1px solid gray;padding:4px}")
with tags.table():
with tags.thead():
tags.th("Test")
tags.th("Pass%")
tags.th("random")
tags.th("Bugs / Notes")
with tags.tbody():
with tags.tr(): #Row 1
tags.td("random", rowspan="5")
tags.td("random", "%", rowspan="5", style="font-size:small;text-align:center;border:1px solid gray;padding:4px;background-color:#66FF66")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr(): # Row 2 (only cols 3 and 4)
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr(): # Row 3
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr(): # Row 4
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
with tags.tr(): # Row 5
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
tags.td("1", style = "font-size:small;text-align:center;padding:4px")
HTML_TMP = "/tmp/demo.html"
MAIL_TMP = "/tmp/demo.txt"
# save it to a file
with open(HTML_TMP, "w") as f:
f.write(str(doc))
生成的HTML和输出在下面(点击取消隐藏,然后运行代码段以查看结果):
<!DOCTYPE html>
<html>
<head>
<title>whatever</title>
</head>
<body><style>body{font-family:Helvetica}</style><style>h1{font-size:x-large}</style><style>h2{font-size:large}</style><style>table{border-collapse
:collapse}</style><style>th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}</style><style>td{font-size:small;text-align:cen
ter;border:1px solid gray;padding:4px}</style>
<table>
<thead>
<th>Test</th>
<th>Pass%</th>
<th>random</th>
<th>Bugs / Notes</th>
</thead>
<tbody>
<tr>
<td rowspan="5">random</td>
<td rowspan="5" style="font-size:small;text-align:center;border:1px solid gray;padding:4px;background-color:#66FF66">random%</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">1</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">1</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">1</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">1</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
</tbody>
</table>
</body>
</html>
注意你可以通过在函数中添加重复的代码并调用它来进一步整理,但我猜这是一个占位符代码,你实际上不会真正添加每个单元格中的相同信息