0

So I want to create a condition that allows me to read two or more file types in google drive using pydrive. But I don't know where to start.

and I do have this code.

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 ''
Jore Dawal
  • 59
  • 1
  • 10

1 Answers1

0

You cannot read those files directly with the Drive or Sheets API, but you can convert them to Google spreadsheets

Use the method Files: insert for Dirve v2 or Files: create for v3.

With pydrive it should look like this:

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})

References:

ziganotschka
  • 25,866
  • 2
  • 16
  • 33