diff --git a/gspread/client.py b/gspread/client.py index a419882f..effd4bd5 100644 --- a/gspread/client.py +++ b/gspread/client.py @@ -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, diff --git a/gspread/http_client.py b/gspread/http_client.py index 7cc802dd..469ee8fd 100644 --- a/gspread/http_client.py +++ b/gspread/http_client.py @@ -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 @@ -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" + elif format == ExportFormat.CSV: + format_str = "csv" + elif format == ExportFormat.ZIPPED_HTML: + format_str = "zip" + 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, diff --git a/gspread/worksheet.py b/gspread/worksheet.py index 6a6783b8..7784195d 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -59,6 +59,7 @@ numericise_all, rowcol_to_a1, to_records, + ExportFormat, ) if TYPE_CHECKING is True: @@ -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,