在为outlook生成的html中编码/解码时出错

时间:2017-11-28 15:37:34

标签: python python-2.7 encoding utf-8

我有2个函数,第一个准备html并写入.txt文件。第二个功能打开此文件并通过Outlook管理电子邮件。

在邮件正文中,此html的内容将以正确的格式放置。一切顺利,.txt带有html而没有任何错误,但是当Outlook打开它时,它被关闭并且下面生成错误/异常:

  

'ascii'编解码器不能编码位置529中的字符u'\ xe7':序数不在范围内(128)

我知道这个\xe7ç,但我无法解决,我已经尝试通过.decode("utf-8").encode("utf-8")在变量中对其进行定义email_html_leitura,但编解码器的错误仍然存​​在。按照2个函数的代码来查看我是否做错了什么:

功能1:

import sys
import codecs
import os.path

def gerar_html_do_email(self):
    texto_solic = u'Solicitação Grupo '
    with codecs.open('html.txt', 'w+', encoding='utf8') as email_html:
        try:
            for k, v in self.dicionario.iteritems():
                email_html.write('<h1>'+k+'</h1>'+'\n')
                for v1 in v:
                    if (v1 in u'Consulte o documento de orientação.') or (v1 in u'Confira o documento de orientação.'):
                        for x, z in self.tit_nome_pdf.iteritems():
                            if x in k:
                                email_html.write('<a href='+'%s/%s'%(self.hiperlink,z+'>')+'Mais detalhes'+'</a>'+'\n')
                    else:
                        email_html.write('<p>'+v1+'</p>'+'\n')
                email_html.write('<p><i>'+texto_solic+'</i></p>'+'\n')
            email_html.close()
        except Exception as erro:
            self.log.write('gerar_html_para_o_email: \n%s\n'%erro)

功能2:

def gerar_email(self):
    import win32com.client as com
    try:
        outlook       = com.Dispatch("Outlook.Application")
        mail          = outlook.CreateItem(0)
        mail.To       = u"Lista Liberação de Versões Sistema"
        mail.CC       = u"Lista GCO"
        mail.Subject  = u"Atualização Semanal Sistema Acrool"
        with codecs.open('html.txt', 'r+', encoding='utf8') as email_html_leitura:
            mail.HTMLBody = """
                            <html>
                                <head></head>
                                <body>
                                    <style type=text/css>
                                        h1{
                                            text-align: center;
                                            font-family: "Arial";
                                            font-size: 1.1em;
                                            font-weight: bold;
                                        }
                                        p{
                                            text-align: justify;
                                            font-family: "Arial";
                                            font-size: 1.1em;
                                        }
                                        a{
                                            font-family: "Arial";
                                            font-size: 1.1em;
                                        }
                                    </style>
                                    %s
                                </body>
                            </html>
                            """%(email_html_leitura.read().decode("utf-8"))
        email_html_leitura.close()
        mail.BodyFormat = 2
        mail.Display(True)
    except Exception as erro:
        self.log.write('gerar_email: \n%s\n'%erro)

0 个答案:

没有答案