Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从google drive读取excel文件而不下载文件_Python_Excel_Google Drive Api - Fatal编程技术网

Python 从google drive读取excel文件而不下载文件

Python 从google drive读取excel文件而不下载文件,python,excel,google-drive-api,Python,Excel,Google Drive Api,我想从google drive上的excel文件中读取excel工作表,而无需在本地机器上下载!我搜索了google drive api,但找不到解决方案我尝试了以下代码请提供建议: ''' from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import pandas as pd gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = Goo

我想从google drive上的excel文件中读取excel工作表,而无需在本地机器上下载!我搜索了google drive api,但找不到解决方案我尝试了以下代码请提供建议:

'''
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import pandas as pd

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file_id = 'abc'
file_name = 'abc.xlsx'  

downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile(file_name)


class TestCase:
  def __init__(self, file_name, sheet):
    self.file_name = file_name
    self.sheet = sheet
    testcase = pd.read_excel(file_name, usecols=None, sheet_name=sheet)
    print(testcase)


class TestCaseSteps:
   def __init__(self, file_name, sheet):
    self.file_name = file_name
    self.sheet = sheet
    testcase = pd.read_excel(file_name, usecols=None, sheet_name=sheet)
    print(testcase)
testcases=TestCase(文件名'A') 步骤=TestCaseSteps(文件名“B”)
''

我相信你的目标和情况如下

  • 您想使用
    pd.read\u excel
    读取从Google Drive下载的XLSX
  • 您希望在不将下载的XLSX数据保存为文件的情况下实现这一点
  • 您的
    gauth=GoogleAuth()
    可用于下载XLSX格式的谷歌电子表格
在这种情况下,我想提出以下流程

  • 以XLSX格式下载谷歌电子表格。
    • 在这种情况下,它直接请求端点使用
      请求
      库将电子表格导出为XLSX格式
    • gauth=GoogleAuth()
      检索访问令牌
  • 下载的XLSX数据通过
    pd.read\u excel
    读取。
    • 在这种情况下,
      BytesIO
      用于读取数据
  • 通过此流程,当电子表格作为XLSX数据下载时,可以读取XLSX数据,而无需将其保存为文件。当上述流程反映到脚本中时,它将变成如下所示

    示例脚本: 在运行脚本之前,请设置电子表格ID

    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    import pandas as pd
    import requests
    from io import BytesIO
    
    spreadsheetId = "###"  # <--- Please set the Spreadsheet ID.
    
    # 1. Download the Google Spreadsheet as XLSX format.
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth()
    url = "https://www.googleapis.com/drive/v3/files/" + spreadsheetId + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
    res = requests.get(url, headers={"Authorization": "Bearer " + gauth.attr['credentials'].access_token})
    
    # 2. The downloaded XLSX data is read with `pd.read_excel`.
    sheet = "Sheet1"
    values = pd.read_excel(BytesIO(res.content), usecols=None, sheet_name=sheet)
    print(values)
    
    从pydrive.auth导入GoogleAuth
    从pydrive.drive导入GoogleDrive
    作为pd进口熊猫
    导入请求
    从io导入字节io
    
    spreadsheetId=“####”#您想在不下载Python的情况下执行此操作吗?或者这个标题是什么意思?编辑:没关系,我是brain afk-你只是想说“在python中”的python实际上我想在google drive上阅读excel文件表(在python中的object),而不用在本地机器上下载。当我使用pandas阅读excel表格时,我需要先将文件从google drive下载到本地机器,然后才能阅读。那么,我可以直接在google drive上读取excel文件,而不是在本地计算机上下载吗?感谢您的帮助,我遇到了以下错误。我试图更改从google工作表源学习的不同方式,但无法确定\n“不支持的格式,或损坏的文件:预期的BOF记录;发现b'{\n“erro”"\n我还检查了enpoint request,它显示了此消息@annonyos感谢您的回复。我为给您带来的不便表示歉意。从您的错误消息中,我认为在您的情况下,XLSX文件已放入Google驱动器,您希望下载。因此我为此添加了一个示例脚本。您能确认吗?如果我误解了您的意思r情况,我再次道歉。@annonyos现在,我修改了下载文件的端点。您能再次确认吗?A)感谢您的帮助,我遇到了以下XLRD错误,试图改变从google工作表源学习的不同方式,但无法找出粗体“不支持的格式,或损坏的文件:预期的BOF记录;找到b”{\n"错误“”粗体B)我还检查了enpoint请求,它显示粗体消息bold@annonyos感谢您的回复。再次为给您带来的不便表示歉意。我认为错误的原因是无法正确下载XLSX数据。在添加脚本后,我已更新了附加脚本的端点。因此,您可以请再次确认?在这种情况下,当
    文件\u id
    是Google Drive上的XLSX文件时,现在我可以确认脚本是否工作。当出现相同错误时,请再次确认文件id。
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    import pandas as pd
    import requests
    from io import BytesIO
    
    file_id = "###"  # <--- Please set the file ID of XLSX file.
    
    # 1. Download the XLSX data.
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth()
    url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
    res = requests.get(url, headers={"Authorization": "Bearer " + gauth.attr['credentials'].access_token})
    
    # 2. The downloaded XLSX data is read with `pd.read_excel`.
    sheet = "Sheet1"
    values = pd.read_excel(BytesIO(res.content), usecols=None, sheet_name=sheet)
    print(values)