Domanda

I'm doing a simple Python (Django) app that reads an Excel file and prints part of it on screen. When I ran it locally with the excel file stored in my local PC it works well.

Here's the code:

from xlrd import open_workbook    
def hello(request):
        wb = open_workbook('test.xlsx')
        sh = wb.sheet_by_index(0)
        a = sh.cell_value(rowx=0, colx=0)
        return HttpResponse(a)

Nevertheless, when I try to read the file from S3 using this:

wb = open_workbook('http://s3.amazonaws.com/mybucketsample/test.xlsx')

I receive the following error:

IOError at / 

[Errno 22] invalid mode ('r') or filename: 'http://s3.amazonaws.com/mybucketsample/test.xlsx'

What am I doing wrong?

Thanks so much,

Ed

È stato utile?

Soluzione

Perhaps this works:

import urllib2

url = 'http://s3.amazonaws.com/mybucketsample/test.xlsx'
filecontent = urllib2.urlopen(url).read()

wb = open_workbook(file_contents=filecontent)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top