से फ़ाइल पढ़ने के लिए openpyxl का उपयोग करके मैंने अजगर में ऑब्जेक्ट के रूप में एक Google-स्प्रेडशीट डाउनलोड की।मेमोरी
मैं ओपनपीएक्सएल का उपयोग पहली बार डिस्क पर सहेजने के बिना कार्यपुस्तिका का उपयोग कैसे कर सकता हूं?
मैं जानता हूँ कि xlrd करके ऐसा कर सकते हैं कि:
book = xlrd.open_workbook(file_contents=downloaded_spreadsheet.read())
"downloaded_spreadsheet" के साथ एक वस्तु के रूप में मेरे डाउनलोड xlsx फ़ाइल जा रहा है।
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..
मुझे आशा है कि किसी को भी मदद कर सकते हैं, क्योंकि मैं महीनों के लिए संघर्ष कर रहा हूँ गूगल-स्प्रेडशीट में दी गई कोशिका से रंग-डेटा प्राप्त करने के। (मैं गूगल-api इसका समर्थन नहीं करता पता ..)
+1 - एक समान गलती की। मैंने केवल पहली छमाही पढ़ी और सोचा कि यह केवल फाइलें पढ़ सकता है। अब मैं वापस गया और इसे पूरी तरह से पढ़ा और देखा कि यह फाइल जैसी वस्तुओं को भी कर सकता है। –