Skip to content

Commit db5c670

Browse files
committed
Add support for Excel sheet name regexps
1 parent fc53cec commit db5c670

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

csvkit/utilities/in2csv.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
2+
import re
33
import sys
44
from os.path import splitext
55

@@ -47,7 +47,9 @@ def option_parser(bytestring):
4747
self.argparser.add_argument('--write-sheets', dest='write_sheets', type=option_parser,
4848
help='The names of the Excel sheets to write to files, or "-" to write all sheets.')
4949
self.argparser.add_argument('--write-sheet-names', dest='write_sheet_names', action='store_true',
50-
help='Use the sheet name to create the target filename.')
50+
help='Use the sheet name to create the target filename when using --write-sheets.')
51+
self.argparser.add_argument('-r', '--regular-expression', dest='regular_expression',
52+
help='Use regular expression to identify XLS sheet names when using --write-sheets.')
5153
self.argparser.add_argument('--encoding-xls', dest='encoding_xls',
5254
help='Specify the encoding of the input XLS file.')
5355
self.argparser.add_argument('-y', '--snifflimit', dest='sniff_limit', type=int,
@@ -162,6 +164,8 @@ def main(self):
162164

163165
if self.args.write_sheets == '-':
164166
sheets = self.sheet_names(path, filetype)
167+
elif self.args.regular_expression:
168+
sheets = [sheet for sheet in self.sheet_names(path, filetype) if re.match(self.args.write_sheets, sheet)]
165169
else:
166170
sheets = [int(sheet) if sheet.isdigit() else sheet for sheet in self.args.write_sheets.split(',')]
167171

docs/scripts/in2csv.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ The header line is required though the columns may be in any order::
4747
--write-sheets WRITE_SHEETS
4848
The names of the Excel sheets to write to files, or
4949
"-" to write all sheets.
50-
--write-sheet-names Use the sheet name to create the target filename.
50+
--write-sheet-names Use the sheet name to create the target filename when using --write-sheets.
51+
-r, --regular-expression
52+
Use regular expression to identify Excel sheet names when using --write-sheets.
5153
-y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
5254
Limit CSV dialect sniffing to the specified number of
5355
bytes. Specify "0" to disable sniffing entirely.

tests/test_utilities/test_in2csv.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,18 @@ def test_convert_xlsx_with_write_sheets_with_names(self):
261261
path = 'examples/sheets_%s.csv' % suffix
262262
if os.path.exists(path):
263263
os.remove(path)
264+
265+
def test_convert_xlsx_with_write_sheets_with_names_and_regexp(self):
266+
try:
267+
self.assertConverted('xlsx', 'examples/sheets.xlsx', 'examples/testxlsx_noinference_converted.csv', ['--no-inference', '-r', '--write-sheets', "[a-z]+", '--write-sheet-names'])
268+
with open('examples/sheets_data.csv', 'r') as f:
269+
with open('examples/testxlsx_noinference_converted.csv', 'r') as g:
270+
self.assertEqual(f.read(), g.read())
271+
self.assertFalse(os.path.exists('examples/sheets_0.csv'))
272+
self.assertFalse(os.path.exists('examples/sheets_1.csv'))
273+
self.assertFalse(os.path.exists('examples/sheets_2.csv'))
274+
finally:
275+
for suffix in ('data',):
276+
path = 'examples/sheets_%s.csv' % suffix
277+
if os.path.exists(path):
278+
os.remove(path)

0 commit comments

Comments
 (0)