Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
warn(
"Character(s) in '{}' are not allowed in a spreadsheet cell. Those character(s) will be removed".format(
value
),
DataErrorWarning,
)
value = new_value
line.append(value)
worksheet.append(line)
def close(self):
self.workbook.remove(self.workbook.active)
self.workbook.save(self.output_name)
class CSVOutput(SpreadsheetOutput):
def open(self):
try:
os.makedirs(self.output_name)
except OSError:
pass
def write_sheet(self, sheet_name, sheet):
sheet_header = list(sheet)
with open(
os.path.join(self.output_name, self.sheet_prefix + sheet_name + ".csv"),
"w",
newline="",
encoding="utf-8",
) as csv_file:
dictwriter = csv.DictWriter(csv_file, sheet_header)
dictwriter.writeheader()
def write_sheet(self, sheet_name, sheet):
sheet_header = list(sheet)
with open(
os.path.join(self.output_name, self.sheet_prefix + sheet_name + ".csv"),
"w",
newline="",
encoding="utf-8",
) as csv_file:
dictwriter = csv.DictWriter(csv_file, sheet_header)
dictwriter.writeheader()
for sheet_line in sheet.lines:
dictwriter.writerow(sheet_line)
class ODSOutput(SpreadsheetOutput):
def open(self):
self.workbook = OpenDocumentSpreadsheet()
def _make_cell(self, value):
""" Util for creating an ods cell """
if value:
try:
# See if value parses as a float
cell = odf.table.TableCell(valuetype="float", value=float(value))
except ValueError:
cell = odf.table.TableCell(valuetype="string")
else:
cell = odf.table.TableCell(valuetype="Nonetype")
p = odf.text.P(text=value)
raise NotImplementedError
def write_sheets(self):
self.open()
self.write_sheet(self.main_sheet_name, self.parser.main_sheet)
for sheet_name, sub_sheet in sorted(self.parser.sub_sheets.items()):
self.write_sheet(sheet_name, sub_sheet)
self.close()
def close(self):
pass
class XLSXOutput(SpreadsheetOutput):
def open(self):
self.workbook = openpyxl.Workbook()
def write_sheet(self, sheet_name, sheet):
sheet_header = list(sheet)
worksheet = self.workbook.create_sheet()
worksheet.title = self.sheet_prefix + sheet_name
worksheet.append(sheet_header)
for sheet_line in sheet.lines:
line = []
for header in sheet_header:
value = sheet_line.get(header)
if isinstance(value, str):
new_value = ILLEGAL_CHARACTERS_RE.sub("", value)
if new_value != value:
warn(