簡體   English   中英

有什么方法可以使用 pydrive 在 Google 驅動器中讀取多種不同的文件類型,例如 xlsv、csv、xls?

[英]Is there any way that i can read multiple different file type like xlsv, csv, xls in Google drive using pydrive?

所以我想創建一個條件,允許我使用 pydrive 在谷歌驅動器中讀取兩種或多種文件類型。 但我不知道從哪里開始。

我確實有這個代碼。

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

 spreadsheet_id = '######'

    #This is to read spreadsheet file
    url = "https://www.googleapis.com/drive/v3/files/" + spreadsheet_id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
    res = requests.get(url, headers={"Authorization": "Bearer " + gauth.attr['credentials'].access_token})

    #This is to read xlsv
    url = "https://www.googleapis.com/drive/v3/files/" + spreadsheet_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 = "Summary"

    values = pd.read_excel(BytesIO(res.content), usecols=None, sheet_name=sheet)
    print (values)


    return ''

您無法直接使用 Drive 或 Sheets API 讀取這些文件,但可以將它們轉換為 Google 電子表格

使用方法Files: insert for Dirve v2 或Files: create for v3。

使用 pydrive 它應該是這樣的:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file = drive.CreateFile({
                        ,'title': 'myNewGoogleSpreadsheet'
                        ,'mimeType':'application/vnd.google-apps.spreadsheet'})
file.SetContentFile('myExcelFile')
file.Upload({'convert': True})

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM