Package: drive
DriveGoogle Drive client.Installpython -m pip install driveWith Poetry:poetry add driveYou also need to havelibmagicinstalled to get automatic detection of uploaded files’ MIME types. If you don’t have
it, you must provide theoriginal_mime_typekeyword argument when you upload a file.
On Linux, if you do havelibmagicbut Python can’t see it, see thisStackOverflow question.UsageThe API exposes a client asdrive.client.Clientthat manipulates instances ofdrive.files.File. AFilerepresent a Google Drive file. Note that both
regular files and directories are represented asFiles, and a file can have
multiple parent directories. You can check if aFileis a directory using theis_directoryattribute.Note: "Folder" is just a synonym for "Directory".AuthenticationBy default, the client reads your service account key JSON file at the location
given by the environment variableGOOGLE_APPLICATION_CREDENTIALS. You can
override this behavior by passing it directly:client = Client("/path/to/your/service-account-key.json")See Google’s documentation onhow to create a service account key.ClientHigh-levelClientmethods:get_file(file_id)(File)get_file_by_name(name)(File)files_shared_with_me()(Filelist)get_shared_directory(name)(File)root()(File)upload_file(parent, path[, name]): Upload a fileupload_excel_workbook(parent, name, workbook): Upload anopenpyxlworkbook in a Google spreadsheet underparentwith the namename.The client also exposes low-level methods that work on file ids.Fileid(str, attribute)name(str, attribute)is_directory(bool, attribute)human_type(str, attribute): Human-readable file typeexists()(bool)unlink()(bool): Remove the file. If it's a directory, all its children
are removed as wellrename(new_name): Rename the filemove_in(new_parent[, new_name]): Move a file under another directory. It
can also rename the file at the same time.list(): List a directory’s contentcreate_folder(name): Create a folder under the current oneget_or_create_folder(name): Retrieve a child folder or create it if it
doesn’t existget_child(name): Return a file under the current directory.parents(): Return a file's parentsparent(): Return the first parent of a filedownload_file(path[, mime_type]): Download the file at a given locationdownload_workbook(): Download the file as anopenpyxlworkbookjson(): Parse the file as JSONjsons(): Parse the file as JSONS (one JSON per line) and returns a generatorMethods that operate on directories (e.g.list()) generally have no effect if
theFileinstance is a regular file.Examplesfromdrive.clientimportClient# Uses credentials from the path in the environment variable# GOOGLE_APPLICATION_CREDENTIALS.cl=Client()# Get the root directoryd=cl.root()print(d.is_directory)# Trueprint(d.name)# e.g. "My Drive"# Get a directory's contentforfind.list():print(f.name)# Get a shared directoryd=cl.get_shared_directory("My Shared Dir")Spreadsheetsfromdrive.clientimportClientfromopenpyxlimportWorkbookcl=Client()# Downloadf=cl.get_file_by_name("my_sheet")workbook=f.download_workbook()# openpyxl workbook# save your download:workbook.save("myfile.xlsx")# Uploadworkbook=Workbook()d=cl.get_shared_directory("My Shared Directory")cl.upload_excel_workbook(d,"my_other_sheet",workbook)Drawingsfromdrive.clientimportClientcl=Client()# download a Drawing in a png imagecl.download_file("11AASomeFileId","localfile.png","image/png")LicenseCopyright © 2016-2023 Baptiste FontaineDistributed under the MIT License.