0

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!

5
  • What are you trying to achieve by "encoding" a .xls file? That makes no sense at all. Commented Feb 4, 2014 at 21:16
  • I'm just trying to troubleshoot that above error that I'm receiving. Commented Feb 5, 2014 at 4:44
  • But you'll obviously get an error if you're trying to do something wrong. So why are you trying to "encode" a .xls file in the first place? Commented Feb 5, 2014 at 16:37
  • I wasn't trying to "encode" the .xls file in the first place. All I want to be able to do is grab the content of the uploaded Excel file in readable form, but when I upload the file without encoding it (via the webpy's library of file upload), I get the error ("<type 'exceptions.UnicodeDecodeError'> at / 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte") for ALL & only for .xls files. After doing some research, I found that some people suggested to encode the file either using utf-8, cp866, etc. If Im approaching this wrong, please share how I can go about this. Commented Feb 10, 2014 at 21:02
  • I have switched from webpy to web2py because webpy does not have the capabilities to upload excel documents, and web2py works fantastically! I should have done more research and known that webpy doesn't have much functionality. Commented Mar 4, 2014 at 19:52

1 Answer 1

0
    print unicode(x['calendar_file'].value, 'utf-8')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.