简体   繁体   English

特定格式的HTML代码

[英]HTML code in a specific format

I have the HTMl code @ http://pastie.org/8456333 as a reference ,also I have the below code where i am construcing HTML code to email ..i need inputs or suggestions on what modifcations need to be done to below code to make the table look like the HTML code @ http://pastie.org/8456333 我有HTMl代码@ http://pastie.org/8456333作为参考,也有下面的代码,在这些代码中,我正在构造HTML代码以通过电子邮件发送..我需要输入或建议对以下代码进行哪些修改使表格看起来像HTML代码@ http://pastie.org/8456333

import re
import os
import sys
import time
from email.mime.text import MIMEText
import subprocess
from subprocess import check_call,Popen,PIPE

def email (body,subject,to=None):
    msg = MIMEText("%s" % body)
    msg["Content-Type"] = "text/html"
    msg["From"] = "cdit@company.com"
    #msg["From"] = "test@company.com"
    if to!=None:
        to=to.strip()
        #msg["To"] = "test@company.com"
        msg["To"] = to
    else:
        msg["To"] = to
    msg["Subject"] = '%s' % subject
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
    p.communicate(msg.as_string())

def  manifest_table(project,branch):
    global table_items
    global tableProperties
    table_items = table_items + "<tr><td>%s</td><td>%s</td></tr>"%(project,branch)
    tableBody = """\
                <style type="text/css">
                %s
                </style>
                <table id="tfhover" class="tftable" border="1">
                %s
                </table>
            """%(tableProperties, table_items)
    return tableBody

def main ():
    i=0
    global table_items
    table_items = "<tr><th>Project</th><th>Branch</th></tr>"
    global tableProperties
    tableProperties = r"table.tftable {font-size:12px;color:#333333;width:10%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;} table.tftable th {font-size:12px;background-color:#ded0b0;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;} table.tftable tr {background-color:#ffffff;} table.tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}"
    project_list=['data','modem','1x','umts']
    branch_list=['jb','fr','kk','choc']
    for proj in project_list:
        branch = branch_list[i]
        mft = manifest_table(proj,branch)
        i=i+1
    email_body = """\
            <html>
              <head></head>
              <body>
                <p>Please find the table for the gerrits you provided:- <br>
                </p>
                %s
              </body>
            </html>
            """%(mft)
    print "Emailing..."
    email(email_body,"Manifest table","test@company.com")

if __name__ == '__main__':
    main()

For making your table I suggest to use string.Template: this is simple example: 为了制作表格,我建议使用string.Template:这是一个简单的示例:

from string import Template
a = Template("$name is my friend")
b=a.substitute(name="Sara")
print b
#output: Sara is my friend

So about a part of your code: 关于代码的一部分:

table_items = table_items + "<tr><td>%s</td><td>%s</td></tr>"%(project,branch)

you can do it better: 您可以做得更好:

table_items += "<tr><td>%(p)s</td><td>%(b)s</td></tr>" % {'p':project,'b':branch}

or via string.Template: 或通过string.Template:

table_items = Template("<tr><td>$p</td><td>$b</td></tr>")
table_items += table_items.substitute(p=project,b=branch)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM