Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Download PDF from Spreadsheet #1035 #1036

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@

from google.auth.transport.requests import AuthorizedSession

from .exceptions import APIError, SpreadsheetNotFound
from .exceptions import APIError, SpreadsheetNotFound, UnSupportedExportFormat
from .spreadsheet import Spreadsheet
from .urls import (
DRIVE_FILES_API_V2_URL,
DRIVE_FILES_API_V3_COMMENTS_URL,
DRIVE_FILES_API_V3_URL,
DRIVE_FILES_UPLOAD_API_V2_URL,
)
from .utils import convert_credentials, extract_id_from_url, finditem
from .utils import (
ExportFormat,
MimeType,
convert_credentials,
extract_id_from_url,
finditem,
)


class Client:
Expand Down Expand Up @@ -84,7 +90,7 @@ def list_spreadsheet_files(self, title=None, folder_id=None):
page_token = ""
url = DRIVE_FILES_API_V3_URL

q = 'mimeType="application/vnd.google-apps.spreadsheet"'
q = 'mimeType="{}"'.format(MimeType.google_sheets)
if title:
q += ' and name = "{}"'.format(title)
if folder_id:
Expand Down Expand Up @@ -193,7 +199,7 @@ def create(self, title, folder_id=None):
"""
payload = {
"name": title,
"mimeType": "application/vnd.google-apps.spreadsheet",
"mimeType": MimeType.google_sheets,
}

params = {
Expand All @@ -207,6 +213,36 @@ def create(self, title, folder_id=None):
spreadsheet_id = r.json()["id"]
return self.open_by_key(spreadsheet_id)

def export(self, file_id, format):
"""Export the spreadsheet in the format.

:param str file_id: A key of a spreadsheet to export

:param str format: The format of the resulting file.
Possible values are
``ExportFormat.PDF``,
``ExportFormat.EXCEL``,
``ExportFormat.CSV``,
``ExportFormat.OPEN_OFFICE_SHEET``,
``ExportFormat.TSV``,
and ``ExportFormat.ZIPPED_HTML``.
See `ExportFormat`_ in the Drive API.

:returns bytes: A content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""

if format not in ExportFormat:
raise UnSupportedExportFormat

url = "{}/{}/export".format(DRIVE_FILES_API_V3_URL, file_id)

params = {"mimeType": format}

r = self.request("get", url, params=params)
return r.content

def copy(
self,
file_id,
Expand Down Expand Up @@ -254,7 +290,7 @@ def copy(

payload = {
"title": title,
"mimeType": "application/vnd.google-apps.spreadsheet",
"mimeType": MimeType.google_sheets,
}

if folder_id is not None:
Expand Down
4 changes: 4 additions & 0 deletions gspread/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"""


class UnSupportedExportFormat(Exception):
"""Raised when export format is not supported."""


class GSpreadException(Exception):
"""A base class for gspread's exceptions."""

Expand Down
21 changes: 21 additions & 0 deletions gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,27 @@ def share(
with_link=with_link,
)

def export(self, format):
"""Export the spreadsheet in the format.

:param str file_id: A key of a spreadsheet to export

:param str format: The format of the resulting file.
Possible values are
``ExportFormat.PDF``,
``ExportFormat.EXCEL``,
``ExportFormat.CSV``,
``ExportFormat.OPEN_OFFICE_SHEET``,
``ExportFormat.TSV``,
and ``ExportFormat.ZIPPED_HTML``.
See `ExportFormat`_ in the Drive API.

:returns bytes: A content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""
return self.client.export(self.id, format)

def list_permissions(self):
"""Lists the spreadsheet's permissions."""
return self.client.list_permissions(self.id)
Expand Down
22 changes: 22 additions & 0 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@
ValueInputOption = namedtuple("_ValueInputOption", ["raw", "user_entered"])(
"RAW", "USER_ENTERED"
)
MimeType = namedtuple(
"_MimeType",
["google_sheets", "pdf", "excel", "csv", "open_office_sheet", "tsv", "zip"],
)(
"application/vnd.google-apps.spreadsheet",
"application/pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text/csv",
"application/vnd.oasis.opendocument.spreadsheet",
"text/tab-separated-values",
"application/zip",
)
ExportFormat = namedtuple(
"_ExportFormat", ["PDF", "EXCEL", "CSV", "OPEN_OFFICE_SHEET", "TSV", "ZIPPED_HTML"]
)(
MimeType.pdf,
MimeType.excel,
MimeType.csv,
MimeType.open_office_sheet,
MimeType.tsv,
MimeType.zip,
)


def convert_credentials(credentials):
Expand Down