【问题标题】:Referring to CSS Stylesheets in Python Code在 Python 代码中引用 CSS 样式表
【发布时间】:2021-12-22 18:30:04
【问题描述】:

我正在开发一个自动电子邮件项目,该项目为我提供了来自被动收入投资的每日收入。我已经完成了所有自动化工作,现在我正在研究电子邮件结构。我的电子邮件中显示的 CSS 元素有问题。这是相关代码。

def send_mail(text=" ", subject=('PKs Daily Overview ' + date), from_email='PKBot <piercedailymail@gmail.com>', to_emails=None, html= """ 
<head>
  <link href="C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\normalize.cs" rel="stylesheet" type="text/css">
  <link href="C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\taegans-groovy-site.webflow.css" rel="stylesheet" type="text/css">
  <link href="C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\webflow.css" rel="stylesheet" type="text/css">
</head>
<body>
  <div class="pricing-section">
    <div class="container">
      <div class="title-block">
        <h1 class="heading">Pierce&#x27;s <span class="text-span">Passive Income Breakdown</span></h1>
      </div>
      <div class="pricing-wrap">
        <div class="pricing-column-02">
          <div class="pricing-card purple">
            <div class="div-block">
              <h3 class="pricing-h3 white">Helium</h3><img src="images/helium-hnt-logo.png" loading="lazy" width="53" id="w-node-_3b1efe47-cc49-dccf-fdc4-c9ae7d3a64ca-c7f2d992" srcset="images/helium-hnt-logo-p-500.png 500w, images/helium-hnt-logo-p-800.png 800w, images/helium-hnt-logo-p-1080.png 1080w, images/helium-hnt-logo-p-1600.png 1600w, images/helium-hnt-logo.png 2000w" sizes="53px" alt="">
            </div>
            <div class="pricing-details-wrap">
              <div class="pricing white"><h1>${strtotal}</h1></div>
              <div class="pricing white"><h1 style="color:purple;">${strtotal}</h1></div>
              <div class="date white">last 24 hours</div>
            </div>
            <div class="pricing-check-wrap">
              <div class="pricing-check">
                <p class="pricing-text white"><strong>Expert Foggy Perch:</strong></p>
              </div>
              <div class="pricing-check">
                <p class="pricing-text white _2">$8.00</p>
              </div>
              <div class="pricing-check">
                <p class="pricing-text white"><strong>Macho Carrot Orca:</strong></p>
              </div>
              <div class="pricing-check">
                <p class="pricing-text white _2">$8.00</p>
              </div>
              <p class="pricing-text white"><strong>Sneaky Chrome Dog:</strong></p>
              <p class="pricing-text white _2">$8.00</p>
            </div>
            <a href="https://explorer.helium.com/accounts/13pm9juR7WPjAf7EVWgq5EQAaRTppu2EE7ReuEL9jpkHQMJCjn9" class="pricing-button white w-button">Open Explorer</a>
          </div>
        </div>
      </div>
    </div>
  </div>
"""

它带有所有的 HTML。 &lt;div class="pricing white"&gt;&lt;h1 style="color:purple;"&gt;${strtotal}&lt;/h1&gt;&lt;/div&gt; 的代码行对我来说只是一个测试,看看 CSS 是否可以内联工作,但我仍然无法引用样式表。感谢任何帮助。 :)

【问题讨论】:

    标签: python html css


    【解决方案1】:

    我认为您不能以这种方式引用您的 css 文件,因为当从接收方读取电子邮件时,它无法从提供的路径中提取 css 文件(因为路径存储在本地计算机上而不是接收者的)。我认为解决此问题的一种方法是将css文件的全部内容粘贴到html代码中,在&lt;style&gt;标签内,而不是使用&lt;string&gt;.format(&lt;value 1&gt;, ...)函数

    def extract_css(css_file_name):
        # return the string content of the CSS file
        with open(css_file_name, 'r') as fl:
            content = fl.read()
        return content
    
    def send_mail(text=" ", subject=('PKs Daily Overview ' + date), from_email='PKBot <piercedailymail@gmail.com>', to_emails=None, html= """
    <head>
      <style>
    {}
      </style>
      <style>
    {}
      </style>
      <style>
    {}
      </style>
    </head>
    <body>
      <div class="pricing-section">
        <div class="container">
          <div class="title-block">
            <h1 class="heading">Pierce&#x27;s <span class="text-span">Passive Income Breakdown</span></h1>
          </div>
          <div class="pricing-wrap">
            <div class="pricing-column-02">
              <div class="pricing-card purple">
                <div class="div-block">
                  <h3 class="pricing-h3 white">Helium</h3><img src="images/helium-hnt-logo.png" loading="lazy" width="53" id="w-node-_3b1efe47-cc49-dccf-fdc4-c9ae7d3a64ca-c7f2d992" srcset="images/helium-hnt-logo-p-500.png 500w, images/helium-hnt-logo-p-800.png 800w, images/helium-hnt-logo-p-1080.png 1080w, images/helium-hnt-logo-p-1600.png 1600w, images/helium-hnt-logo.png 2000w" sizes="53px" alt="">
                </div>
                <div class="pricing-details-wrap">
                  <div class="pricing white"><h1>${strtotal}</h1></div>
                  <div class="pricing white"><h1 style="color:purple;">${strtotal}</h1></div>
                  <div class="date white">last 24 hours</div>
                </div>
                <div class="pricing-check-wrap">
                  <div class="pricing-check">
                    <p class="pricing-text white"><strong>Expert Foggy Perch:</strong></p>
                  </div>
                  <div class="pricing-check">
                    <p class="pricing-text white _2">$8.00</p>
                  </div>
                  <div class="pricing-check">
                    <p class="pricing-text white"><strong>Macho Carrot Orca:</strong></p>
                  </div>
                  <div class="pricing-check">
                    <p class="pricing-text white _2">$8.00</p>
                  </div>
                  <p class="pricing-text white"><strong>Sneaky Chrome Dog:</strong></p>
                  <p class="pricing-text white _2">$8.00</p>
                </div>
                <a href="https://explorer.helium.com/accounts/13pm9juR7WPjAf7EVWgq5EQAaRTppu2EE7ReuEL9jpkHQMJCjn9" class="pricing-button white w-button">Open Explorer</a>
              </div>
            </div>
          </div>
        </div>
      </div>
      """):
    
        # html with stylesheet code included
        sent_html = html.format(
                extract_css('C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\normalize.css'),
                extract_css('C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\taegans-groovy-site.webflow.css'),
                extract_css('C:\\Users\\kings\\PythonProjects\\DailyEmail\\css\\webflow.css'),
                strtotal='some string')
        print(sent_html)
        # Do something
    

    【讨论】:

    • 在将整个 CSS 文件粘贴到 HTML 代码中时遇到了很多问题。您是说接收方无法访问 CSS 文件,因为它们存储在本地。有没有一种方法可以打印包含 CSS 引用的 html 文件,因为它们已经被存储了?
    • 在将 css 文件粘贴到 HTML 代码时遇到什么问题?我认为您不能使用 CSS 参考打印呈现的 html,因为 html 仅在接收器打开时由浏览器呈现
    • 它给了我所有 CSS 属性的 /n KeyErrors。
    • 您能否编辑您的帖子以包含您尝试使用此方法的代码和完整的错误消息?我认为这可能有助于调试问题
    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2014-11-23
    • 2016-10-16
    相关资源
    最近更新 更多