When I use Google Drive API to download my Excel files, it will send and outdated version. The way to reproduce this is to open the Excel file in Google Sheets Editor, than add new tabs, then download the File through the API. Most of the time it will not send the file with the latest added Tab.
I'm having this issue using the Google build('drive', 'v3', credentials=creds)
in my project, but could also reproduce this with a simpler script that I got here. It uses requests and it's way simpler:
import os
import requests
def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download&confirm=1"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
if __name__ == "__main__":
file_id = '1JI17N-NFAIOxX_2Y88gmuKMlsuGhBPcB'
home = os.getenv('HOME')
destination = f'{home}/Downloads/test-drive-download/output.xlsx'
download_file_from_google_drive(file_id, destination)
The file id shown in the script is public: https://docs.google.com/spreadsheets/d/1JI17N-NFAIOxX_2Y88gmuKMlsuGhBPcB/edit#gid=1677858863
I've also recorded a video for better clarification: https://drive.google.com/file/d/1-L_SnWp1zQNWJ34Z2JtVbgnPVIONuBgr/view?usp=sharing