使用openpyxl从内存读取文件 [英] Using openpyxl to read file from memory

查看:286
本文介绍了使用openpyxl从内存读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中下载了一个谷歌电子表格作为对象。

I downloaded a google-spreadsheet as an object in python.

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

How can I use openpyxl use the workbook without having it to save to disk first?

我知道xlrd可以这样做:

I know that xlrd can do this by:

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

与uploaded_spreadsheet是我下载的xlsx文件作为对象。

with "downloaded_spreadsheet" being my downloaded xlsx-file as an object.

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

Instead of xlrd, I want to use openpyxl because of better xlsx-support(I read).

我目前使用这个...

I'm using this so far...

#!/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 = "........@gmail.com" # (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电子表格中的给定单元格中获取颜色数据。 (我知道google-api不支持..)

I hope anyone can help because I'm struggling for months to get the color-data from given cell in google-spreadsheet. (I know the google-api doesn't support it..)

推荐答案

load_workbook 它说:

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

..所以它一直有能力。它读取路径或采取类似文件的对象。
我只需要将 urlopen 返回的类似文件的对象转换为 bytestream ,其中: / p>

..so it was capable of it all the time. It reads a path or takes a file-like object. I only had to convert my file-like object returned by urlopen, to a bytestream with:

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

,我可以阅读我的Google电子表格中的每一个数据。

and I can read every piece of data in my Google-spreadsheet.

这篇关于使用openpyxl从内存读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆