Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:

return self.http_client.export(file_id=file_id, format=format)

def export_worksheet(self, file_id, sheet_id: str, format: str = ExportFormat.PDF) -> bytes:
"""Export the spreadsheet in the given format.

:param str file_id: The key of the spreadsheet to export

:param str sheet_id: The key of the worksheet 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``
* ``ExportFormat.ZIPPED_HTML``

See `ExportFormat`_ in the Drive API.

:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""
return self.http_client.export_worksheet(file_id, sheet_id, format)

def copy(
self,
file_id: str,
Expand Down
49 changes: 49 additions & 0 deletions gspread/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
SPREADSHEET_VALUES_BATCH_URL,
SPREADSHEET_VALUES_CLEAR_URL,
SPREADSHEET_VALUES_URL,
SPREADSHEET_DRIVE_URL,
)
from .utils import ExportFormat, convert_credentials, quote

Expand Down Expand Up @@ -358,6 +359,54 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:
r = self.request("get", url, params=params)
return r.content

def export_worksheet(self, file_id, sheet_id: str, format: str = ExportFormat.PDF) -> bytes:
"""Export the worksheet in the given format.

:param str file_id: The key of the spreadsheet to export

:param str sheet_id: The key of the worksheet 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``
* ``ExportFormat.ZIPPED_HTML``

See `ExportFormat`_ in the Drive API.

:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

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

if format not in ExportFormat:
raise UnSupportedExportFormat

format_str = "pdf"
if format == ExportFormat.PDF:
format_str = "pdf"
Comment on lines +391 to +393
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the default value is PDf, then you don't have to check if it's PDF in format

elif format == ExportFormat.CSV:
format_str = "csv"
elif format == ExportFormat.ZIPPED_HTML:
format_str = "zip"
Comment on lines +396 to +397
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one does not work.
as well as you don't handle the value: OPEN_OFFICE_SHEET which results as PDF due to default value set at the start of the ifs here.

elif format == ExportFormat.EXCEL:
format_str = "xlsx"
elif format == ExportFormat.TSV:
format_str = "tsv"

url = "{}/export?gid={}&format={}".format(SPREADSHEET_DRIVE_URL % file_id, sheet_id, format_str)

params: ParamsType = {"mimeType": format}

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

def insert_permission(
self,
file_id: str,
Expand Down
24 changes: 24 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
numericise_all,
rowcol_to_a1,
to_records,
ExportFormat,
)

if TYPE_CHECKING is True:
Expand Down Expand Up @@ -295,6 +296,29 @@ def _get_sheet_property(self, property: str, default_value: Optional[T]) -> T:

return sheet.get(property, default_value)

def export(self, format=ExportFormat.PDF):
"""Export the worksheet in the given format.

:param 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.
Default value is ``ExportFormat.PDF``.
:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

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

def acell(
self,
label: str,
Expand Down