2016-11-28 3 views
1

xlsxwriter를 사용하여 xlsx 파일을 생성 한 다음 전자 메일의 첨부 파일로 보내려고합니다.Django : 바이트와 유사한 객체를 전자 메일에 첨부하지 못하도록하는 TypeError

def WriteToExcel(project): 
    output = BytesIO() 
    workbook = xlsxwriter.Workbook(output) 

    #putting in data 

    workbook.close() 
    xlsx_data = output.getvalue() 
    # xlsx_data contains the Excel file 
    return xlsx_data 

def project_email (request, project_id): 
    project = Project.objects.get(id = project_id) 
    xlsx_data = WriteToExcel(project) 

    message = EmailMessage("Heading", 'Here is the message.', 'HOST', ['[email protected]']) 

    message.attach_file(xlsx_data) 
    message.send() 

를 내가 이메일을 보내려고 할 때, 나는 다음과 같은 오류가 있습니다 :

TypeError at /projstatus/1/email

cannot use a string pattern on a bytes-like object

내가 주변에 갈 수있는 방법이 있나요 여기에 내가 지금 가지고있는 무엇인가? xlsx 파일을 비 바이너리로 만들거나 전자 메일에 바이너리 파일을 첨부 할 수있는 함수가 있습니까?

답변

1

사실이 문제를 발견했습니다.

def project_email (request, project_id): 
    project = Project.objects.get(id = project_id) 
    xlsx_data = WriteToExcel(project) 

    message = EmailMessage("Heading", 'Here is the message.', 'HOST', ['[email protected]']) 

    message.attach("Report.xlsx", xlsx_data, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') 
    message.send() 

나는 단지의 경우 다른 사람에 여기를 떠나 것 같은 일

를 궁금해 : 당신은 같은 것을 필요
관련 문제