Python Forum
Unable to read data from string base64 representing a .xlsx file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to read data from string base64 representing a .xlsx file
#1
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\n
To 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"
]
Reply
#2
You most likely need to remove the data:;base64, part in xlStr. You can try
xlStr = xlStr.split(',', 1)[1]
xlDecoded = base64.b64decode(xlStr)
Reply
#3
(Apr-05-2018, 04:55 PM)Gribouillis Wrote: You most likely need to remove the data:;base64, part in xlStr. You can try
xlStr = xlStr.split(',', 1)[1]
xlDecoded = base64.b64decode(xlStr)

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....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 2,869 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,429 Nov-09-2023, 10:56 AM
Last Post: mg24
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  read file txt on my pc to telegram bot api Tupa 0 1,106 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,105 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,512 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,195 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,588 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020