将excel文件附加到内存中的电子邮件(不保存到系统)

时间:2016-01-15 19:17:28

标签: python pandas attachment mandrill

我使用pandas从几个数据框生成一个excel文件:

def to_excel(x, y, z): 

    filename = 'x_y_z_' + date.today().strftime('%Y-%m-%d')

    writer = pd.ExcelWriter(filename, engine='openpyxl')
    x.to_excel(writer, 'x')
    y.to_excel(writer, 'y')
    z.to_excel(writer, 'z')
    writer.close()

    return writer

这是我们用于为Mandrill

创建消息的函数
def create_message(variables, from_name, from_email, to_addrs, bcc_addrs, subject, tags=None, images=None, attachments=None):
    """ Create template_content and message to use with mandrill. 
    :param variables: dict or list of variables
    :param str from_email:
    :param str to_addrs: 
    :param str subject:
    :param list tags:
    """
    template_content = []

    if type(variables) == dict:
        global_merge_vars = [{'name': key, 'content': value} for key, value in variables.iteritems()]
    elif type(variables) == list:
        global_merge_vars = variables
    else:
        print 'Variables type is not list or dictionary. Please update variables'
        sys.exit()

    message = {
        'from_email': from_email,
        'from_name': from_name,
        'to': [{'email': x, } for x in to_addrs] + [{'email': x, 'type': 'bcc'} for x in bcc_addrs], 
        'subject': subject,
        'preserve_recipients': True,
        'merge': True,
        'merge_language': "handlebars",
        'global_merge_vars': global_merge_vars,
        'tags': tags,
        'images':images,
        'attachments':attachments
    }

    return template_content, message

这是发生的错误。 TypeError: <pandas.io.excel._Openpyxl22Writer object at 0x10efe7210> is not JSON serializable

0 个答案:

没有答案