![]() |
Unable to read data from string base64 representing a .xlsx file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Unable to read data from string base64 representing a .xlsx file (/thread-9384.html) |
Unable to read data from string base64 representing a .xlsx file - max - Apr-05-2018 Im struggling so much. And my deadline is tomorrow T_T I have a string base64 containing a full excel (.xlsx) report and i need to be able to read it. The process i've made so far is the following. import xlrd import base64 import io xlStr = "data:;base64,UEsDBBQACAgIAC57hEwAAA[...]" xlDecoded = base64.b64decode(xStr) #type of decoded file is <class 'bytes'> so im going to create a tempFile to produce a stream xlFile = open('temp.xlsx', 'wb') xlFile.write(xlDecoded) #Last line got printed an unknown number (for me): 8788 xlFile.close() xlFile = open('temp.xlsx', 'rb') # At this point, xlFile.readline() will produce the following message: #b"u\xabZm\xab\x1e\xeb\x85\x04\xb00A@\x00\x80\x80\x80\x02\xe7\xb8D\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x80\x00\x00\x07\x86\xc2\xf6G&\x17v\x96\xe6w2\xf6G&\x17v\x96\xe6s\x12\xe7\x86\xd6\xc9\xdd\x05\x14\xec3\x00\xc0n\x017\x08r\xae\xf6\xb5\xa0F\x85N\xbfe'\x10#\x88\x04\x9d\xc3e\xa9\xd5Glk\xad\xb1=\x11RG\x81\x97\xdbB\xc7\xff\xaf\xde\xf0\xe10\xdd\x90{+\x8e\x06\xb5^h\\\xad\t\xb6\t\xde\xf6\xaf_o\xab-\x9a\xa4\xc2'\x80\xb4?\x05\x8a\xb0\xb8\xa3\xae\xce\xf7i>^\xa2\xc0\xd6~\x9d\xe4\xb9\\e\xafS\x18\xe9]f'\xa2I\x03\xc8\xce\x8d;`\xd4\xc1\r<\x89\xdb`\xcerM:\x0e\xf8\xbe$\x9c\xbc\x80\x85g\xacM\x8c\xc1\xb7_V\x08Tn\x0f\xc7'\xf59\xad\x0bl\xe6\x01<\xc8\x9d\x0cq\x96\x11\xc2\nTo decode last message, i tried decoding and encondings usin utf-8, latin-1, ascii, and i got all kind of responses, sometimes the text didnt change at all, sometimes it changed a little (first characters), sometimes i got errors, and sometimes i got really wierd characters. The thing is that i'm probably making mistakes since i got the "xlDecoded", i dont know how to make that byte-like object to become a xlsx. Once with the xlsx the idea was using xlrd to do the task, but i wasnt able to pass the byte-like object. Maybe the string base64 is corrupted, but ive tried with a lot of other excels too. My frontend is made in angular, so there i may have the problem at encoding the file in base64. Im using FileReader to do the task, so it shouldnt be there. Maybe my approach is wrong and it won't work this way. Any ideas how to get the data out from string base64? PD:[ "Python 3.6", "This backend is inside a lambda function on AWS", "Lot of people strongly recomended me to use S3 but, but im running out of time and i have no idea how to implement S3, nor implement python to use S3, nor use angular to upload to S3... I am afraid it could take me more than 3 or 4 days" ] RE: Unable to read data from string base64 representing a .xlsx file - Gribouillis - Apr-05-2018 You most likely need to remove the data:;base64, part in xlStr. You can tryxlStr = xlStr.split(',', 1)[1] xlDecoded = base64.b64decode(xlStr) RE: Unable to read data from string base64 representing a .xlsx file - max - Apr-05-2018 (Apr-05-2018, 04:55 PM)Gribouillis Wrote: You most likely need to remove the You have made my day sir.... that solved the problem and saved my ass :'): xlStr = xlStr.split(',', 1)[1] xlDecoded = base64.b64decode(xlStr) xlsx = xlrd.open_workbook(file_contents=xlDecoded) firstSheet = xlsx.sheet_by_index(0) print(firstSheet.cell(1,0).value) #It printed and worked perfectly as intended.... |