Skip to content

Commit

Permalink
fix(in2csv): --write-sheets no longer errors when standard input is a…
Browse files Browse the repository at this point in the history
…n XLS or XLSX file
  • Loading branch information
jpmckinney committed Dec 21, 2023
1 parent ed05def commit 88753f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Unreleased
----------

* :doc:`/scripts/in2csv` adds a :code:`--reset-dimensions` option to `recalculate <https://openpyxl.readthedocs.io/en/stable/optimized.html#worksheet-dimensions>`_ the dimensions of an XLSX file, instead of trusting the file's metadata. csvkit's dependency `agate-excel <https://agate-excel.readthedocs.io/en/latest/>`_ 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 <https://openpyxl.readthedocs.io/en/stable/optimized.html#worksheet-dimensions>`_ the dimensions of an XLSX file, instead of trusting the file's metadata. csvkit's dependency `agate-excel <https://agate-excel.readthedocs.io/en/latest/>`_ 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
------------------------
Expand Down
12 changes: 10 additions & 2 deletions csvkit/utilities/in2csv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import functools
import sys
from io import BytesIO
from os.path import splitext
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 88753f9

Please sign in to comment.