Created
July 26, 2019 13:23
-
-
Save DataSolveProblems/9cb6e09f2d167190b3c2f3cd4b704a89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from Google import Create_Service | |
import pandas as pd | |
working_dir = r'<>' | |
output_dir = os.path.join(working_dir, 'Output') | |
CLIENT_SECRET_FILE = r"client_secret.json" | |
API_SERVICE_NAME = 'drive' | |
API_VERSION = 'v3' | |
SCOPES = ['https://www.googleapis.com/auth/drive'] | |
service = Create_Service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES) | |
files_service = service.files() | |
query_body = "mimeType='application/vnd.google-apps.spreadsheet'" | |
files_retrieved = files_service.list( | |
q=query_body, | |
pageSize=5 | |
).execute() | |
df = pd.DataFrame(files_retrieved.get('files')) | |
next_page_token = files_retrieved.get('nextPageToken') | |
while next_page_token: | |
files_retrieved = files_service.list( | |
q=query_body, | |
pageToken=next_page_token | |
).execute() | |
df = pd.concat([df, pd.DataFrame(files_retrieved.get('files'))]) | |
next_page_token = files_retrieved.get('nextPageToken') | |
print(f'Token: {next_page_token}') | |
EXCEL_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | |
for i in range(len(df)): | |
byteData = files_service.export( | |
fileId=df.iloc[i]['id'], | |
mimeType=EXCEL_MIME_TYPE | |
).execute() | |
filename = df.iloc[i]['name'] | |
print(filename) | |
with open(os.path.join(output_dir, filename + '.xlsx'), 'wb') as f: | |
f.write(byteData) | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment