使用python xlrd模块

时间:2018-04-24 19:46:25

标签: python excel amazon-s3 urllib xlrd

我使用以下代码使用Amazon S3 pythonxlrd模块从urllib读取excel文件,但我收到Forbidden访问错误。我知道这是因为我没有通过AWS Access KeyAWS Secret Access Key。我环顾四周找到了将密钥作为urllib参数传递的方法,但无法找到示例。

import urllib.request
import xlrd

url = 'https://s3.amazonaws.com/bucket1/final.xlsx'
filecontent = urllib.request.urlopen(url).read()

workbook = xlrd.open_workbook(file_contents=filecontent)
worksheet = workbook.sheet_by_name(SheetName)

如何使用python xlrd模块从S3读取excel?

1 个答案:

答案 0 :(得分:0)

这可以使用boto API

完成
import boto
import boto.s3.connection
from boto.s3.key import Key
import sys
import pandas as pd

    try:
           conn = boto.connect_s3(aws_access_key_id = your_access_key, aws_secret_access_key = your_secret_key)
           bucket = conn.get_bucket('your_bucket')
           print ("connected to AWS/s3")
    except Exception as e:
           print ("unable to connect to s3 - please check credentials")
           print(e)
           sys.exit(1)

destFileName = "/tmp/myFile.xlsx"
k = Key(bucket, "path_to_file_on_s3/sourceFile.xlsx")
k.get_contents_to_filename(destFileName)

df = pd.read_excel(destFileName, sheet_name=Sheet1)
print(df.head())