From 88753f9f1f756a64ccacae53897b8f85cdfef995 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:54:12 -0500 Subject: [PATCH] fix(in2csv): --write-sheets no longer errors when standard input is an XLS or XLSX file --- CHANGELOG.rst | 3 ++- csvkit/utilities/in2csv.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7216302b6..613fcc708 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,8 @@ Unreleased ---------- -* :doc:`/scripts/in2csv` adds a :code:`--reset-dimensions` option to `recalculate `_ the dimensions of an XLSX file, instead of trusting the file's metadata. csvkit's dependency `agate-excel `_ 0.4.0 automatically recalculates the dimensions if the file's metadata expresses dimensions of "A1:A1" (a single cell). +* feat: :doc:`/scripts/in2csv` adds a :code:`--reset-dimensions` option to `recalculate `_ the dimensions of an XLSX file, instead of trusting the file's metadata. csvkit's dependency `agate-excel `_ 0.4.0 automatically recalculates the dimensions if the file's metadata expresses dimensions of "A1:A1" (a single cell). +* fix: :doc:`/scripts/in2csv`: :code:`--write-sheets` no longer errors when standard input is an XLS or XLSX file. 1.3.0 - October 18, 2023 ------------------------ diff --git a/csvkit/utilities/in2csv.py b/csvkit/utilities/in2csv.py index 9ee188e18..57bc75e68 100644 --- a/csvkit/utilities/in2csv.py +++ b/csvkit/utilities/in2csv.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import functools import sys from io import BytesIO from os.path import splitext @@ -63,9 +64,13 @@ def add_arguments(self): '-I', '--no-inference', dest='no_inference', action='store_true', help='Disable type inference (and --locale, --date-format, --datetime-format) when parsing CSV input.') + @functools.lru_cache + def stdin(self): + return sys.stdin.buffer.read() + def open_excel_input_file(self, path): if not path or path == '-': - return BytesIO(sys.stdin.buffer.read()) + return BytesIO(self.stdin()) return open(path, 'rb') def sheet_names(self, path, filetype): @@ -186,7 +191,10 @@ def main(self): self.input_file, sheet=sheets, reset_dimensions=self.args.reset_dimensions, **kwargs ) - base = splitext(self.input_file.name)[0] + if not path or path == '-': + base = 'stdin' + else: + base = splitext(self.input_file.name)[0] for i, (sheet_name, table) in enumerate(tables.items()): if self.args.use_sheet_names: filename = '%s_%s.csv' % (base, sheet_name)