I'm trying to create a web application that lets users upload an .xls file that I then take and feed that uploaded.xls file into my program which reads and parses it. I am currently using Python 2.7 on the Web.py framework.
However, I am having issues with the utf-8 encoding for the Excel files. This method seems to be only working for .txt & .csv files, but when I try images or .pdf they don't work, so I'm not sure if the web.py built in library just doesn't support Excel files. When I upload an Excel file, it just spits out unreadable content like the following:
■ ♠☺☻ ☺ ☻╒═╒£.←►ô +,∙«0 ░ ☺ H ↨ P ♂ X ♀ ï ☻ Σ♦ ♥ ♫ ♂ ♂ ♂ ♂ ▲► ☺ Sheet1 ▲ ♂ Worksheets ♥ ☺
Here is my code:
class index:
def POST(self):
x = web.input(calendar_file={}, ref_id='')
if x:
ref_id = (x.ref_id if x.ref_id else "")
filepath=x.calendar_file.filename # replaces the windows-style slashes with linux ones.
fn=filepath.split('/')[-1] # splits the and chooses the last part (the filename
filename = "%s/Users/jl98567/Documents/xMatters_calendar_app/test/" + fn
fullpath = os.path.join('c:', filename % (ref_id))
content = x["calendar_file"].file.read()
with open(fullpath, 'w') as f_out:
if not f_out:
raise Exception("Unable to open %s for writing. " % (fullpath))
f_out.write(content)
print x['calendar_file'].value
raise web.seeother('/upload?ref_id=%s&filename=%s' % (ref_id, filename))
Now, when I try to encode:
print x['calendar_file'].value.encode('utf-8')
I get the following error:
at / 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
The weird thing is that I know encoding it to utf-8 works on my application that isn't web based or using the web.py file upload method. So I can't seem to see what the problem is here.
For example:
content = str(sheet.cell(row,0).value.encode('utf8'))
that works perfectly fine using the xlrd and xlwt python-excel methods.
Any suggestions?
Thanks much!