【问题标题】:How to add html styling in html page in python如何在python的html页面中添加html样式
【发布时间】:2015-07-15 13:12:53
【问题描述】:

我正在尝试设置使用 Python 创建的 HTML 页面的样式,但不知何故我没有得到它。

以下是我没有样式标签的代码

htmlfile = open(fruit+".html")
htmlfile.write("<html>\n")
htmlfile.write("<head>\n")
htmlfile.write("<title> homepage </title>\n")
htmlfile.write("</head>\n")
htmlfile.write("<body>\n")
htmlfile.write("<h1> <center> <b> {color:red;} Fruit-properties </b> </center> </h1>\n")
htmlfile.write("</body>\n") 

有人知道如何在 python 中添加样式标签吗?

【问题讨论】:

  • ....考虑使用合适的模板引擎? stackoverflow.com/questions/2955615/python-template-engine
  • 我想过。但 html 只是大代码的一小部分。所以我希望在当前代码本身中添加样式标签。
  • htmlfile.write("

    &lt;font color="red"&gt; Fruit-properties &lt;/font&gt;

    \n")
  • 谢谢。它在红字处显示无效语法
  • 那是因为你需要转义“red”这个词周围的双引号,否则 Python 会假定你的 HTML 字符串正好在那个词之前结束。您还可以对实际的 python 字符串使用单引号(即')或三引号。查看docs on strings

标签: python html


【解决方案1】:

HTML 样式是使用CSS 完成的。首先你需要学习 CSS 基础知识。

然后您可以提供单独的 CSS 样式文件或内联样式 HTML 代码:

  htmlfile.write("<h1 style='color: red; text-align: center'>Fruit-properties</h1>\n")

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2021-06-29
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多