Package: gdrive-tools
GDrive ToolsGoal of this ProjectThe automated managing of google drive documents is quite laborious.
This is because of the Google Drive API v3, which does not allow to pass
a directory path of a document that should be created.
Instead, documents are only ordered using the parents node id.Since its more common for us tothinkin directory trees, its
easier to specify a full path.This Project offers methods to manage document by providingrealpath
specifications rather then a child - parent relationship for each document.
(For example: Its way more convenient to specify a path by writing something
likepath/to/my/documentthan searching for the parent Id of the document).UsageThe usage of this library should be straight forward.Firstly, you have to create aGDriveToolsClient- Object which only needs your
credentials. This can be archived like so:importgdrive_tools.gdrive_toolsasgtimportgdrive_tools.google_authasgafromgdrive_tools.google_filetypesimportGoogleFiletypesSCOPES=['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/documents','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/presentations']# Create a google auth object which wraps the authentication on the google# drive api.auth=ga.GoogleAuth(SCOPES)credentials=auth.createCredentials()# Create the api client and pass the read credentials.googleDriveToolsClient=gt.GoogleDriveTools(credentials)CapabilitiesWith the GDrive tools library, you can do the following things:Create a new DocumentA new document can be created using thecreateFile()method. The created
document will be placed inside a directory with the given path. Any nonexisting
directories will be created.Directories which are placed in the drives trash folder will be ignored.The following
parameters are needed:destination(str): Full path, where the document should be moved to.
All directories are delimited by a simple slash (/).
If the document should be created inside a shared drive, the name of the shared
drive should be provided first. (Its basically seen as the root directory.)
Example:MySharedDrive/subdirectory/anotherSubdirectoryIf you want to create a new document on your local drive, the first
entry is also the first sub folder. For example:subdirectory/anotherSubdirectorydocumentName(str): Name of the Document that should be created.fileType(int): Type of the document. Currently, the following types are
supported:GoogleFiletypes.DOCUMENT: Google Docs fileGoogleFiletypes.SHEET: Google Sheets fileGoogleFiletypes.SLIDE: Google Slides fileThere are also different keyword arguments which can be used, to modify the
created documents. The following ones are currently supported:`sheetTableName(str): Specify a custom name which should be used for the first Sheet,
when creating a new sheet.The return value is the id of the created document.Move a DocumentA document can be moved from one directory to another, either inside your
local, shared drive or between shared drives, using themoveDocument()method.The following parameters are needed.sourcePath(str): Full path, of the document, which should be moved.
All directories are delimited by a simple slash (/).
If the document should be moved inside or to a shared drive, the name of the shared
drive should be provided first.
Example:MySharedDrive/subdirectory/anotherSubdirectory/targetFilenameIf you want to create a new document on your local drive, the first
entry is seen as the first subdirectory. Example:subdirectory/anotherSubdirectory/targetFilenamedestinationPath(str): The target path where the document should be
moved to. The described syntax of thesourcePathparameter also applies here.The method returns the id of the moved document.Copy a DocumentIts also possible to copy a document into another directory and/or to another
team drive. This can
be done by using thecopyDocument()- Method.The following Parameters are required:sourcePath(str): The full source path of the document that should be copied.
The syntax is equivalent to the syntax of themoveDocument()Method.destinationPath(str): The path which defines where the copy of the source
document should be created.The method returns the id of the copied document.Fill a SheetIf you want to fill a google documents sheet, you can use thefillSheet()Method.Keep in mind that any existing data inside the sheet will be overwritten.The method takes the following parameters:sheetId(str): The Id of the sheet which should be filled.data(List[dict])The data which should be inserted into the sheet as a list
of dictionaries.
The Columns are therefore defined by the keys of the dictionaries, whereas
all dictionaries must have the same keys.[sheetTableName(str)='']The name of the table inside the given sheet, where the data should
be inserted.
If the table does not exists, aValueErrorwill be thrown.Read Data from a SheetWith thereadSheet()method you can read the data from a sheet. The method
will return a list of dictionaries which contains the sheet's rows and columns.The target sheet has to contain only a single table without extra cells. Its assumed
that the first row defines the names of the columns.
However, its possible to specify a custom range in the A1 notation. This range
defines, which data should be read.The method takes the following parameters:sheetId(str): The id of the target sheet.sheetName(str): The name of the table/sheet which should be used.[a1Range(str)]: A custom range which points to the data which should be read.
Since the sheet name is already passed with the sheetName property, you can't
also specify it here.[placeholder(dict)]: A dictionary which contains placeholder values for each
column, which is not defined.A column is also considered as "not defined" if the value is an empty string.Grant PermissionsYou can grant Permissions to a given document by using thegrandApproval()Method.The method takes the following parameters:sheetId(str): The Id of the document, that should be shared with a user.email(str): The EMail address of the user, which should gain access to the document.accessLevel(GoogleAccessLevel): The type of permission which should be grant to the
user.[grantType(GoogleGrantTypes)]: You can set a custom grant type by using this property.Please note that if you are using the grant typeDOMAIN, theemailparameter has to contain the name of the target domain.[emailText(str)='']: An optional text which should be embedded inside the
email notification which is automatically send by google, if a user gained access to a document.Read all Files from a DirectoryIf you want to retrieve a list of all files in a given directory, you can use
thereadDirectory()method. As the name suggests, this method returns a
list of all files which are included in this directory.The return value of this method is a directory, which has the following keys:directory_id: ID of the directoryfiles: List of files, found inside this directory.Whereas each file - directory has the following properties:name: Name of the fileid: ID of the filetype: filetypeThe method only needs one parameter:path(str): The path of the directory which should be read. The syntax is the
same as in themoveDocument()orcopyDocument()methods.This method returns the dictionary, which is described above.AValueErroris thrown, if the given directory does
not exists.Return the Id of a DocumentIf you just want to know the Id of a document which is saved in a given path,
you can query it by using thegetDocumentId()method.This method only takes one parameter:Args:path(str): The path to the document, whose Id should be
returned.Returns:
The Id of the document, which is stored on the provided path, or an empty
string, if the document does not exists.ExampleYou can test the library using the givenexample.pyscript.Enable the Google Drive and Docs Api and Download the Client ConfigurationIn order for the script to work, its required to have a validcredentials.jsonfile.
The example script only needs an activated GoogleDrive and GoogleSheets api.Navigate tohttps://developers.google.com/drive/api/v3/quickstart/pythonClick on theEnable the Drive ApiButtonNavigate tohttps://developers.google.com/docs/api/quickstart/pythonClick on theEnable the Docs ApiButtonClick onDownload Client ConfigurationButtonMove the downloadedcredentials.jsonfile to this directory.Install the DependenciesThere are two ways how to install the dependencies.Using PipIf you use pip natively, you can simply install the dependencies from
therequirements.txtfile usingpip install -r requirements.txt.Using PipenvThis project's dependencies can also be managed with Pipenv. To use Pipenv, make
sure its installed on your system (if not it can be done so by executingpip install pipenv).
Then you can install the dependencies usingpipenv install.To run the example script in the virtual environnement which was created by pipenv, you can
runpipenv run python example.py.