How to drop Google Docs in one folder in a .txt file with python

things to do

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

1. Environment construction

Download Drive API access key

Drop the access key json from here Press the blue button in the image below and it's ok

image.png

Change the file name of the access key

File name credentials.jsonclient_secret.json Change to

Reason: The PyDrive package looks for an access key file with the name client_secret.json

Insert the PyDrive package

Enter 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

2. Get the Google Drive folder ID

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.

3. Write code

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

4. Run

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

How to drop Google Docs in one folder in a .txt file with python
How to read a CSV file with Python 2/3
How to create a JSON file in Python
How to convert / restore a string with [] in python
How to check ORM behavior in one file with django
How to import a file anywhere you like in Python
How to work with BigQuery in Python
How to create a heatmap with an arbitrary domain in Python
How to get a stacktrace in python
How to combine all CSVs in a folder into one CSV
[ROS2] How to play a bag file with python format launch
How to display legend marks in one with Python 2D plot
How to calculate "xx time" in one shot with Python timedelta
Read a Python # .txt file for a super beginner in Python with a working .py
[Python] Get the files in a folder with Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
[Python] How to read excel file with pandas
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to draw a histogram in Matplotlib
I want to write to a file with Python
How to read a file in a different directory
Sample to put Python Kivy in one file
How to get a list of files in the same directory with python
Parse a JSON string written to a file in Python
[Python] How to draw a line graph with Matplotlib
How to do hash calculation with salt in Python
Explain in detail how to make sounds with python
How to do zero-padding in one line with OpenCV
How to run tests in bulk with Python unittest
[Python] How to expand variables in a character string
How to load files in Google Drive with Google Colaboratory
A memorandum to run a python script in a bat file
How to measure mp3 file playback time with python
I want to randomly sample a file in Python
How to convert JSON file to CSV file with Python Pandas
I want to work with a robot in python.
[Work efficiency] How to change file names in Python
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
Run a Python file with relative import in PyCharm
[Python] How to draw a scatter plot with Matplotlib
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
How to develop in Python
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
How to embed multiple embeds in one message with Discord.py
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to install NPI + send a message to line with python
[Python] Create a file & folder path specification screen with tkinter
How to convert an array to a dictionary with Python [Application]
How to run a Python file at a Windows 10 command prompt
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
[Python] How to write an if statement in one sentence.