Drive API Wrapper

class pygsheets.drive.DriveAPIWrapper(http, data_path, retries=3, logger=<Logger pygsheets.drive (WARNING)>, request_builder=None)[source]

A simple wrapper for the Google Drive API.

Various utility and convenience functions to support access to Google Drive files. By default the requests will access the users personal drive. Use enable_team_drive(team_drive_id) to connect to a TeamDrive instead.

Only functions used by pygsheet are wrapped. All other functionality can be accessed through the service attribute.

See reference for details.

Parameters:
  • http – HTTP object to make requests with.
  • data_path – Path to the drive discovery file.
include_items_from_all_drive = None

Include files from TeamDrive and My Drive when executing requests.

enable_team_drive(team_drive_id)[source]

Access TeamDrive instead of the users personal drive.

disable_team_drive()[source]

Do not access TeamDrive (default behaviour).

get_update_time(file_id)[source]

Returns the time this file was last modified in RFC 3339 format.

list(**kwargs)[source]

Fetch metadata of spreadsheets. Fetches a list of all files present in the users drive or TeamDrive. See Google Drive API Reference for details.

Reference: Files list request

Parameters:kwargs – Standard parameters (see documentation for details).
Returns:List of metadata.
create_folder(name, folder=None, **kwargs)[source]

Create a new folder

Parameters:
  • name – The name to give the new folder
  • folder – The id of the folder this one will be stored in
  • kwargs – Standard parameters (see documentation for details).
Returns:

The new folder id

get_folder_id(name)[source]

Fetch the first folder id with a given name

Parameters:name – The name of the folder to find
folder_metadata(query='', only_team_drive=False)[source]

Fetch folder names, ids & and parent folder ids.

The query string can be used to filter the returned metadata.

Reference: search parameters docs.

Parameters:query – Can be used to filter the returned metadata.
spreadsheet_metadata(query='', only_team_drive=False, fid=None)[source]

Fetch spreadsheet titles, ids & and parent folder ids.

The query string can be used to filter the returned metadata.

Reference: search parameters docs.

Parameters:
  • fid – id of file [optional]
  • query – Can be used to filter the returned metadata.
delete(file_id, **kwargs)[source]

Delete a file by ID.

Permanently deletes a file owned by the user without moving it to the trash. If the file belongs to a Team Drive the user must be an organizer on the parent. If the input id is a folder, all descendants owned by the user are also deleted.

Reference: delete request

Parameters:
  • file_id – The Id of the file to be deleted.
  • kwargs – Standard parameters (see documentation for details).
move_file(file_id, old_folder, new_folder, body=None, **kwargs)[source]

Move a file from one folder to another.

Requires the current folder to delete it.

Reference: update request

Parameters:
  • file_id – ID of the file which should be moved.
  • old_folder – Current location.
  • new_folder – Destination.
  • body – Other fields of the file to change. See reference for details.
  • kwargs – Optional arguments. See reference for details.
copy_file(file_id, title, folder, body=None, **kwargs)[source]

Copy a file from one location to another

Reference: update request

Parameters:
  • file_id – Id of file to copy.
  • title – New title of the file.
  • folder – New folder where file should be copied.
  • body – Other fields of the file to change. See reference for details.
  • kwargs – Optional arguments. See reference for details.
update_file(file_id, body=None, **kwargs)[source]

Update file body.

Reference: update request

Parameters:
  • file_id – ID of the file which should be updated.
  • body – The properties of the file to update. See reference for details.
  • kwargs – Optional arguments. See reference for details.
export(sheet, file_format, path='', filename='')[source]

Download a spreadsheet and store it.

Exports a Google Doc to the requested MIME type and returns the exported content.

Warning

This can at most export files with 10 MB in size!

Uses one or several export request to download the files. When exporting to CSV or TSV each worksheet is exported into a separate file. The API cannot put them into the same file. In this case the worksheet index is appended to the file-name.

Reference: request

Parameters:
  • sheet – The spreadsheet or worksheet to be exported.
  • file_format – File format (ExportType)
  • path – Path to where the file should be stored. (default: current working directory)
  • filename – Name of the file. (default: Spreadsheet Id)
create_permission(file_id, role, type, **kwargs)[source]

Creates a permission for a file or a TeamDrive.

See reference for more details.

Parameters:
  • file_id – The ID of the file or Team Drive.
  • role – The role granted by this permission.
  • type – The type of the grantee.
  • emailAddress – The email address of the user or group to which this permission refers.
  • domain – The domain to which this permission refers.
  • allowFileDiscovery – Whether the permission allows the file to be discovered through search. This is only applicable for permissions of type domain or anyone.
  • expirationTime

    The time at which this permission will expire (RFC 3339 date-time). Expiration times have the following restrictions:

    • They can only be set on user and group permissions
    • The time must be in the future
    • The time cannot be more than a year in the future
  • emailMessage – A plain text custom message to include in the notification email.
  • sendNotificationEmail – Whether to send a notification email when sharing to users or groups. This defaults to true for users and groups, and is not allowed for other requests. It must not be disabled for ownership transfers.
  • supportsTeamDrives – Whether the requesting application supports Team Drives. (Default: False)
  • transferOwnership – Whether to transfer ownership to the specified user and downgrade the current owner to a writer. This parameter is required as an acknowledgement of the side effect. (Default: False)
  • useDomainAdminAccess – Whether the request should be treated as if it was issued by a domain administrator; if set to true, then the requester will be granted access if they are an administrator of the domain to which the item belongs. (Default: False)
Returns:

Permission Resource

list_permissions(file_id, **kwargs)[source]

List all permissions for the specified file.

See reference for more details.

Parameters:
  • file_id – The file to get the permissions for.
  • pageSize – Number of permissions returned per request. (Default: all)
  • supportsTeamDrives – Whether the application supports TeamDrives. (Default: False)
  • useDomainAdminAccess – Request permissions as domain admin. (Default: False)
Returns:

List of Permission Resources

delete_permission(file_id, permission_id, **kwargs)[source]

Deletes a permission.

See reference for more details.
Parameters:
  • file_id – The ID of the file or Team Drive.
  • permission_id – The ID of the permission.
  • supportsTeamDrives – Whether the requesting application supports Team Drives. (Default: false)
  • useDomainAdminAccess – Whether the request should be treated as if it was issued by a domain administrator; if set to true, then the requester will be granted access if they are an administrator of the domain to which the item belongs. (Default: false)