Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

python 3.x - How to solve the error return binascii.a2b_base64(s) binascii.Error: Incorrect padding?

I got this error when I am creating excel file and csv file. The error shows in the line purchase_id = self.env['purchase.excel.extended'].create(attach_files). How can I resolve the issue?

Below is my code:

for data in datas:       
            record = ';'.join(data)
            output.write(record)
            output.write("
")
        data = base64.b64encode(bytes(output.getvalue(),"utf-8"))
        print('data', data)

        if platform.system() == 'Linux':
            excel_file1 = ('/tmp/Purchase Report-' + str(datetime.today().date()) + '.xls')
            csv_file1 = ('/tmp/Purchase Report-' + str(datetime.today().date()) + '.csv')

        else:
            excel_file1 = ('Purchase Report-' + str(datetime.today().date()) + '.xls')
            csv_file1 = ('Purchase Report-' + str(datetime.today().date()) + '.csv')
            
        excel_file1 = excel_file1.split('/')[2]
        csv_file1 = csv_file1.split('/')[2]
        
        workbook.save(excel_file1)
        fp = open(excel_file1, "rb")
        file_data = fp.read()
        out = base64.encodestring(file_data)  
                                                    
# Files actions         
        attach_files = {
                'excel_file': excel_file1,
                'file_name': out,
                'csv_file': csv_file1,
                'file_names': data,
            } 
        print(attach_files, 'attach_files')
                      
        purchase_id = self.env['purchase.excel.extended'].create(attach_files)
        fp.close()
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'purchase.excel.extended',
            'res_id': purchase_id,
            'view_mode': 'form',
            'context': self.env.context,
            'target': 'new',
        }
question from:https://stackoverflow.com/questions/65843608/how-to-solve-the-error-return-binascii-a2b-base64s-binascii-error-incorrect-p

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think that you have the excel file name and the excel file content swapped. Similarly with the CSV file.

attach_files = {
                'excel_file': excel_file1,
                'file_name': out,
                'csv_file': csv_file1,
                'file_names': data,
            }

Should be:

attach_files = {
                'excel_file': out,
                'file_name': excel_file1,
                'csv_file': data,
                'file_names': csv_file1,
            } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...