猿问

在电子邮件正文中显示 Python HTML 表格

我编写了一个 python 脚本来查询我的数据库并以 HTML 表格格式显示数据。我怎样才能让这个代码以表格的形式显示到电子邮件中?


我尝试将代码粘贴在第二个脚本(EMAIL)上的 html 标记中,但它不会仅读取 HTML 的 Python 代码。


            import pyodbc

            import cgi


            def htmlTop():

                print("""Content-type:text/html\n\n

                      <!DOCTYPE html>

                      <html>

                      <head>

                        <meta charset="utf-8"/>

                        <title>My Tabmle</title>

                        </head>

                        <body>""")

            def selectCOAStatus(cnxn, cursor):

                cursor.execute('''SELECT * from mytable''')

                data = cursor.fetchall()

                return data


            def htmlTail():

                print("""</body>

                    </html>""")


            def connectDB():

                conn_str = (

                    r'DRIVER={SQL Server};'

                    r'SERVER=test;'

                    r'DATABASE=test;'

                    r'Trusted_Connection=yes;'

                )

                cnxn = pyodbc.connect(conn_str)

                cursor = cnxn.cursor()

                return cnxn, cursor




            def displayData(data):

                print("<table border='1'")

                print("<tr>")

                print("<th>Date</th>")

                print("<th>Count</th>")

                print("<th>Status</th>")

                print("</tr>")


                for row in data:

                    print("<tr>")

                    print("<td>{0}</td>".format(row[0]))

                    print("<td>{0}</td>".format(row[1]))

                    print("<td>{0}</td>".format(row[2]))

                    print("</tr>")

                print("</table>")


HUWWW
浏览 594回答 2
2回答

元芳怎么了

尝试print("<td>%s</td>" % row[0] )也是一种更简单的 html 电子邮件方法from mailer import Mailerfrom mailer import Messagemessage = Message(From="me@example.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; To="you@example.com")message.Subject = "An HTML Email"message.Html = """<p>Hi!<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;How are you?<br></p>"""sender = Mailer('smtp.example.com')sender.send(message)

精慕HU

我能够将数据放入表中。我修复了我的 for 循环以遍历所有行并返回值。它只返回一行,而我的查询返回多行。为什么会这样?导入pyodbc 导入cgi 导入html&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn_str = (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r'DRIVER=test'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r'SERVER=test;'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r'DATABASE=test;'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r'Trusted_Connection=yes;'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnxn = pyodbc.connect(conn_str)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor = cnxn.cursor()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.execute('''SELECT * from my table '''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for row in cursor:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html_code = """&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!doctype html>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <html>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>Untitled Document</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table border='1'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Count</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Status</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </html>""".format(row[0],row[1],row[2])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(html_code)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import smtplib&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from email.mime.multipart import MIMEMultipart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from email.mime.text import MIMEText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # me == my email address&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # you == recipient's email address&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; me = "test@aol.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; you = "test@aol.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Create message container - the correct MIME type is&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multipart/alternative.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg = MIMEMultipart('alternative')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg['Subject'] = "Link"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg['From'] = me&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg['To'] = you&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Create the body of the message (a plain-text and an HTML version).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "Hi!\nHow are you?\nHere is the link you&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wanted:\nhttp://www.python.org"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html = html_code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Record the MIME types of both parts - text/plain and text/html.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; part1 = MIMEText(text, 'plain')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; part2 = MIMEText(html, 'html')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Attach parts into message container.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # According to RFC 2046, the last part of a multipart message, in this&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # the HTML message, is best and preferred.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg.attach(part1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg.attach(part2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Send the message via local SMTP server.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = smtplib.SMTP('email.fpl.com')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # sendmail function takes 3 arguments: sender's address, recipient's&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # and message to send - here it is sent as one string.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.sendmail(me, you, msg.as_string())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.quit()
随时随地看视频慕课网APP

相关分类

Python
我要回答