The program provides a Google Drive directory scan and writes data to a text file suitable for use with the Visio TreeView template.

The program uses the Google API Client for Python - google-api-python-client.

Terms of use:

  1. Python 3.4 or later version.
  2. Google API Client for Python.
  3. Authorized access to the Google Drive area.
  4. Service Account with access to Google Drive area.

For the subsequent automatic charting, you also need: the desktop version of Visio and the Visio TreeView template 2019.

Installation:

Download archive and unzip the program. Instead, you can save the program text below to a file with the .py extension.

Install the google-api-python-client as written in https://github.com/googleapis/google-api-python-client.

Preparation for use:

  1. Create a service account.
  2. Download the JSON file containing the service account key to your work computer.
  3. Provide access to read the necessary folders and files for the service account email.
  4. Open the program for edit and change the path to the service account key file. If necessary, change the name of the output file.

Now the program is ready to read the folder structure of the Google Drive into a text file.

Everyday use:

Run the program and get the output file.

Use the Visio TreeView template to automatically build a chart.

Output file format

The output file contains 4 columns, separated by commas. An example of the output file is given below:

ID,Name,Parent,isFolder
1,file 2019,0,1
2,EST_DB_RQ.xlsx,0,1
3,first,0,0
4,Getting started,0,1
5,test_write.txt,3,1

Program text:

from google.oauth2 import service_account
from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
from googleapiclient.discovery import build
import pprint
import io
import csv
pp = pprint.PrettyPrinter(indent=4)
SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'C:/temp/API test 37748-8af1690a7cf7.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE,scopes=SCOPES)
service = build('drive', 'v3', credentials=credentials)
results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name, mimeType, parents)").execute()
nextPageToken = results.get('nextPageToken')
while nextPageToken:
  nextPage = service.files().list(pageSize=10,
      fields="nextPageToken, files(id, name, mimeType, parents)",
      pageToken=nextPageToken).execute()
  nextPageToken = nextPage.get('nextPageToken')
results['files'] = results['files'] + nextPage['files']
pp.pprint(results)
flist = results['files']
i = 1
dic = {}
for fl in flist:
  dic[fl['id']] = i
  i = i+1
pp.pprint(dic)
with open('DriveList.csv', 'w', newline='') as csv_file:
  csv_writer = csv.writer(csv_file,dialect='excel')
  csv_writer.writerow(['ID','Name','Parent','isFolder'])
  for item in flist:
    try:
      f = '0'
      fold = item['mimeType'].find('folder')
      if fold == -1:
        f = '1'
      p = '0'
      if 'parents' in item:
        parkey = item['parents'][0]
        if parkey in dic:
          p = dic[parkey]
      csv_writer.writerow([dic[item['id']],item['name'],p,f])
    except:
      print('Error')


Important links:

Program text in ZIP archive

Download Google API Client for Python

Download Visio TreeView template 2019 or see description.