From 2c9f6722010e7ad6a4f4d3215317459eae66aa11 Mon Sep 17 00:00:00 2001 From: Dilshod Temirkhodjaev Date: Tue, 7 May 2024 17:50:18 +0500 Subject: [PATCH] make the 'no line breaks option' apply to inline strings as well --- xlsx2csv.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/xlsx2csv.py b/xlsx2csv.py index 741194a..579565c 100755 --- a/xlsx2csv.py +++ b/xlsx2csv.py @@ -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): @@ -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"([^<>]+) ([^<>]+)", r"\1\\n\2", re.sub(r"([^<>]+) ([^<>]+)", r"\1\\t\2", @@ -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 = "" @@ -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 @@ -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