如何使用 dominate 标签库在 td 中使用 rowspan?

How to use rowspan in td using dominate tags library?

正如您在下面的代码中看到的,我正在生成一个包含 4 列的 HTML table。第一列和第二列有 1 行,第三列和第四列应该有 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))

你能告诉我怎么做吗?

我认为您的问题出在 HTML 结构上。您想要从主 table 中删除嵌入的 tables,并只为第一行的前两个单元格提供属性 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>

注意您可以通过将重复代码添加到函数中并调用它来进一步整理,但我猜这是一个占位符代码,您不会实际上是在每个单元格中添加完全相同的信息