Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct order of arguments to a regex call in safe_xlsx_sheet_title #510

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Here is a list of passed and present much-appreciated contributors:
Luke Lee
Marc Abramowitz
Marco Dallagiacoma
Maris Nartiss
Mark Rogers
Mark Walling
Mathias Loesch
Expand Down
16 changes: 12 additions & 4 deletions src/tablib/formats/_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
INVALID_TITLE_REGEX = re.compile(r'[\\*?:/\[\]]')

def safe_xlsx_sheet_title(s, replace="-"):
return re.sub(INVALID_TITLE_REGEX, s, replace)[:31]
return re.sub(INVALID_TITLE_REGEX, replace, s)[:31]


class XLSXFormat:
Expand Down Expand Up @@ -57,15 +57,23 @@ def export_set(cls, dataset, freeze_panes=True, invalid_char_subst="-"):
return stream.getvalue()

@classmethod
def export_book(cls, databook, freeze_panes=True):
"""Returns XLSX representation of DataBook."""
def export_book(cls, databook, freeze_panes=True, invalid_char_subst="-"):
"""Returns XLSX representation of DataBook.

If dataset.title contains characters which are considered invalid for an XLSX file
sheet name (http://www.excelcodex.com/2012/06/worksheets-naming-conventions/), it will
marisn marked this conversation as resolved.
Show resolved Hide resolved
be replaced with `invalid_char_subst`.
"""

wb = Workbook()
for sheet in wb.worksheets:
wb.remove(sheet)
for i, dset in enumerate(databook._datasets):
ws = wb.create_sheet()
ws.title = dset.title if dset.title else 'Sheet%s' % (i)
ws.title = (
safe_xlsx_sheet_title(dset.title, invalid_char_subst)
if dset.title else 'Sheet%s' % (i)
)

cls.dset_sheet(dset, ws, freeze_panes=freeze_panes)

Expand Down
18 changes: 16 additions & 2 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,22 @@ def test_xlsx_import_set_skip_lines(self):
self.assertEqual(new_data.headers, ['id', 'name', 'description'])

def test_xlsx_bad_chars_sheet_name(self):
data.title = "this / is / good"
_xlsx = data.xlsx
"""
Sheet names are limited to 30 chars and following chars
marisn marked this conversation as resolved.
Show resolved Hide resolved
are not permitted: \\ / * ? : [ ]
"""
_dataset = tablib.Dataset(
title='bad name \\/*?:[]qwertyuiopasdfghjklzxcvbnm'
)
_xlsx = _dataset.export('xlsx')
new_data = tablib.Dataset().load(_xlsx)
self.assertEqual(new_data.title, 'bad name -------qwertyuiopasdfg')

_book = tablib.Databook()
_book.add_sheet(_dataset)
_xlsx = _book.export('xlsx')
new_data = tablib.Databook().load(_xlsx, 'xlsx')
self.assertEqual(new_data.sheets()[0].title, 'bad name -------qwertyuiopasdfg')

def test_xlsx_import_set_ragged(self):
"""Import XLSX file when not all rows have the same length."""
Expand Down