使用openpyxl从内存中读取文件

2024-01-24

我下载了一个 google-spreadsheet 作为 python 中的对象。

如何使用 openpyxl 使用工作簿而不先将其保存到磁盘?

我知道 xlrd 可以通过以下方式做到这一点:

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

“downloaded_spreadsheet”是我下载的 xlsx 文件作为对象。

我想使用 openpyxl 而不是 xlrd,因为它有更好的 xlsx 支持(我读过)。

到目前为止我正在使用这个...

#!/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] /cdn-cgi/l/email-protection" # (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 不支持它..)


在文档中load_workbook它说:

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

..所以它一直都有能力。它读取路径或获取类似文件的对象。 我只需要转换我返回的类似文件的对象urlopen, to a bytestream with:

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

我可以读取 Google 电子表格中的每一条数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用openpyxl从内存中读取文件 的相关文章

随机推荐