There is a folder called some_folder
in Google Drive,
Suppose you have a Google Docs file in your folder like ↓
Create a Python program that drops all of these doc1
, doc2
, ... in .txt
format
some_folder
└ doc1
└ doc2
└ doc3
└ ...
Use a Google Drive API wrapper package called PyDrive
: warning: No error handling when there are files other than Google Docs (SpreadSheet or PDF) in the folder
Drop the access key json from here Press the blue button in the image below and it's ok
File name
credentials.json
→client_secret.json
Change to
Reason: The PyDrive
package looks for an access key file with the name client_secret.json
PyDrive
packageEnter with pip
or pip3
#pip person
pip install PyDrive
#pip3 person
pip3 install PyDrive
(reference)
--Package site: https://pypi.org/project/PyDrive/ --Documentation: https://pythonhosted.org/PyDrive/index.html
You can find the folder ID from the URL when you open the Google Drive folder in your browser.
https://drive.google.com/drive/folders/xxx
The URL has a format like ↑, and the xxx
part is the folder ID.
Paste this and put the ID of the folder you got in 2 in the code FOLDER_ID
: warning: Put the program in the same directory as the client_secret.json
in 1.
download.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
#OAuth
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
#Download the file
file_list = drive.ListFile(
{'q': "'FOLDER_ID' in parents and trashed=false"}).GetList()
for file in file_list:
title = file['title']
file.GetContentFile(f'{title}.txt', mimetype='text/plain')
print(f'downloading file: {title}')
Please run the program either
#python command person
python download.py
#python3 command person
python3 download.py
When you run it, a browser tab will open, so please authenticate with your Google account. The downloaded file will be in the same directory as the program
End: tada:
Recommended Posts