2013-12-17 31 views
11

pythonでオブジェクトとしてgoogle-spreadsheetをダウンロードしました。openpyxlを使用してメモリからファイルを読み取る

openpyxlを使用してブックに保存せずにワークブックを使用するにはどうすればよいですか?

私はxlrdは、これを行うことができることを知っている:「downloaded_spreadsheetは」オブジェクトとしての私のダウンロードのxlsxファイルであることを

book = xlrd.open_workbook(file_contents=downloaded_spreadsheet.read()) 

xlrdの代わりに、より良いxlsx-support(私が読んでいる)のためにopenpyxlを使いたいと思います。

私は

#!/usr/bin/python 

    import openpyxl 
    import xlrd 
    # which to use..? 


import re, urllib, urllib2 

class Spreadsheet(object): 
    def __init__(self, key): 
     super(Spreadsheet, self).__init__() 
     self.key = key 

class Client(object): 
    def __init__(self, email, password): 
     super(Client, self).__init__() 
     self.email = email 
     self.password = password 

    def _get_auth_token(self, email, password, source, service): 
     url = "https://www.google.com/accounts/ClientLogin" 
     params = { 
     "Email": email, "Passwd": password, 
     "service": service, 
     "accountType": "HOSTED_OR_GOOGLE", 
     "source": source 
     } 
     req = urllib2.Request(url, urllib.urlencode(params)) 
     return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0] 

    def get_auth_token(self): 
     source = type(self).__name__ 
     return self._get_auth_token(self.email, self.password, source, service="wise") 

    def download(self, spreadsheet, gid=0, format="xls"): 

     url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i" 
     headers = { 
     "Authorization": "GoogleLogin auth=" + self.get_auth_token(), 
     "GData-Version": "3.0" 
     } 
     req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers) 
     return urllib2.urlopen(req) 

if __name__ == "__main__": 



    email = "[email protected]" # (your email here) 
    password = '.....' 
    spreadsheet_id = "......" # (spreadsheet id here) 

    # Create client and spreadsheet objects 
    gs = Client(email, password) 
    ss = Spreadsheet(spreadsheet_id) 

    # Request a file-like object containing the spreadsheet's contents 
    downloaded_spreadsheet = gs.download(ss) 


    # book = xlrd.open_workbook(file_contents=downloaded_spreadsheet.read(), formatting_info=True) 

    #It works.. alas xlrd doesn't support the xlsx-funcionality that i want... 
    #i.e. being able to read the cell-colordata.. 

は私がグーグル・スプレッドシート内の特定のセルからの色データを取得するために数ヶ月のために苦労していますので、誰でも役立つことを願って...これまでのところ、これを使用しています。 (私はGoogleの-APIがそれをサポートしていません知っている...)

答えて

21

load_workbookのためのドキュメントでは、それは言う:

#:param filename: the path to open or a file-like object 

は、それがすべての時間、それが可能であった..so。パスを読み込むか、ファイルのようなオブジェクトを取得します。

from io import BytesIO 
wb = load_workbook(filename=BytesIO(input_excel.read())) 

と私は私のグーグル・スプレッドシートのデータのすべての部分を読むことができます: 私は私のファイルのようなオブジェクトがでbytestreamに、urlopenによって返さ変換する必要がありました。

+0

+1 - 同様の誤りがありました。私は最初の半分だけを読んで、それはファイルを読むことができると思った。今度は戻ってそれを完全に読んで、それがファイルのようなオブジェクトを同様に行うことができることを見ました。 –

1

は、実際には十分にある。

file = open('path/to/file.xlsx', 'rb') 
wb = openpyxl.load_workbook(filename=file) 

、それが動作します。 BytesIOやものの必要はありません。

+1

問題が示すように、ファイルシステムから読み込まれていません。それは流れです。 –

関連する問題