Skip to content

Commit

Permalink
make the 'no line breaks option' apply to inline strings as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dilshod committed May 7, 2024
1 parent b18479a commit 2c9f672
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions xlsx2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ def __init__(self, xlsxfile, **options):
self.workbook.relationships = self._parse(Relationships, workbook_relationships[0])
else:
self.workbook.relationships = Relationships()
if self.options['no_line_breaks']:
self.shared_strings.replace_line_breaks()
elif self.options['escape_strings']:
if self.options['escape_strings']:
self.shared_strings.escape_strings()

def __del__(self):
Expand Down Expand Up @@ -365,6 +363,7 @@ def _convert(self, sheet_index, outfile):
sheet.set_scifloat(self.options['scifloat'])
sheet.set_ignore_formats(self.options['ignore_formats'])
sheet.set_skip_hidden_rows(self.options['skip_hidden_rows'])
sheet.set_no_line_breaks(self.options['no_line_breaks'])
if self.options['escape_strings'] and sheet.filedata:
sheet.filedata = re.sub(r"(<v>[^<>]+)&#10;([^<>]+</v>)", r"\1\\n\2",
re.sub(r"(<v>[^<>]+)&#9;([^<>]+</v>)", r"\1\\t\2",
Expand Down Expand Up @@ -674,6 +673,7 @@ def __init__(self, workbook, sharedString, styles, filehandle):
self.mergeCells = {}
self.ignore_formats = []
self.skip_hidden_rows = False
self.no_line_breaks = False

self.colIndex = 0
self.colNum = ""
Expand Down Expand Up @@ -704,6 +704,9 @@ def set_ignore_formats(self, ignore_formats):
def set_skip_hidden_rows(self, skip_hidden_rows):
self.skip_hidden_rows = skip_hidden_rows

def set_no_line_breaks(self, no_line_breaks):
self.no_line_breaks = no_line_breaks

def set_merge_cells(self, mergecells):
if not mergecells:
return
Expand Down Expand Up @@ -958,6 +961,9 @@ def handleEndElement(self, name):
else:
d = self.mergeCells[self.mergeCells[self.colNum + self.rowNum]['copyFrom']]['value']

if self.no_line_breaks:
d = d.replace("\r", " ").replace("\n", " ").replace("\t", " ")

self.columns[t - 1 + self.colIndex] = d
self.in_cell = False

Expand Down

0 comments on commit 2c9f672

Please sign in to comment.