From ae1b7f211ea47f56867b11e9be16172a828a08b1 Mon Sep 17 00:00:00 2001 From: Vadym Shevchenko Date: Tue, 16 Jul 2024 23:20:21 +0300 Subject: [PATCH 1/2] Export only one worksheet by it's Id from all document added --- gspread/client.py | 27 ++++++++++++++++++++++ gspread/http_client.py | 52 ++++++++++++++++++++++++++++++++++++++++++ gspread/worksheet.py | 24 +++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/gspread/client.py b/gspread/client.py index a419882f2..effd4bd50 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 7cc802dd2..4c4ece68a 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,57 @@ 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 + + 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 6a6783b80..7784195de 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, From ba809e9d292c841433510c503da55a340320da53 Mon Sep 17 00:00:00 2001 From: Vadym Shevchenko Date: Thu, 18 Jul 2024 01:28:08 +0300 Subject: [PATCH 2/2] fix duplicate code --- gspread/http_client.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/gspread/http_client.py b/gspread/http_client.py index 4c4ece68a..469ee8fdf 100644 --- a/gspread/http_client.py +++ b/gspread/http_client.py @@ -385,9 +385,6 @@ def export_worksheet(self, file_id, sheet_id: str, format: str = ExportFormat.PD .. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats """ - if format not in ExportFormat: - raise UnSupportedExportFormat - if format not in ExportFormat: raise UnSupportedExportFormat