Skip to content

Commit f59edbc

Browse files
committed
Fixed a bug that prevented saving if read_excel was used with use_openpxl_style=True, fixes #67
1 parent 0d7ae44 commit f59edbc

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

styleframe/style_frame.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,14 @@ def get_range_of_cells(row_index=None, columns=None):
398398
try:
399399
style_to_apply = column.style.to_openpyxl_style()
400400
except AttributeError:
401-
style_to_apply = column.style
401+
style_to_apply = Styler.from_openpyxl_style(column.style, [],
402+
openpyxl_comment=column.style.comment).to_openpyxl_style()
402403
column_header_cell = sheet.cell(row=startrow + 1, column=col_index + startcol + 1)
403404
column_header_cell.style = style_to_apply
404405
if isinstance(column.style, Styler):
405406
column_header_cell.comment = column.style.generate_comment()
406407
else:
407-
if hasattr(column.style, 'comment'):
408-
column.style.comment.parent = None
408+
if hasattr(column.style, 'comment') and column.style.comment is not None:
409409
column_header_cell.comment = column.style.comment
410410
for row_index, index in enumerate(self.data_df.index):
411411
current_cell = sheet.cell(row=row_index + startrow + 2, column=col_index + startcol + 1)
@@ -421,13 +421,13 @@ def get_range_of_cells(row_index=None, columns=None):
421421
try:
422422
style_to_apply = data_df_style.to_openpyxl_style()
423423
except AttributeError:
424-
style_to_apply = data_df_style
424+
style_to_apply = Styler.from_openpyxl_style(data_df_style, [],
425+
openpyxl_comment=data_df_style.comment).to_openpyxl_style()
425426
current_cell.style = style_to_apply
426427
if isinstance(data_df_style, Styler):
427428
current_cell.comment = data_df_style.generate_comment()
428429
else:
429-
if hasattr(data_df_style, 'comment'):
430-
data_df_style.comment.parent = None
430+
if hasattr(data_df_style, 'comment') and data_df_style.comment is not None:
431431
current_cell.comment = data_df_style.comment
432432
except AttributeError: # if the element in the dataframe is not Container creating a default style
433433
current_cell.style = Styler().to_openpyxl_style()

styleframe/tests/style_frame_tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,22 @@ def test_read_excel_with_style_openpyxl_objects(self):
352352
for row_in_excel, row_in_self in zip(rows_in_excel, rows_in_self)
353353
for excel_cell, self_cell in zip(row_in_excel[1:], row_in_self[1:])))
354354

355+
def test_read_excel_with_style_openpyxl_objects_and_save(self):
356+
self.export_and_get_default_sheet(save=True)
357+
sf_from_excel = StyleFrame.read_excel(TEST_FILENAME, read_style=True, use_openpyxl_styles=True)
358+
# making sure content is the same
359+
self.assertTrue(all(list(self.sf[col]) == list(sf_from_excel[col]) for col in self.sf.columns))
360+
361+
rows_in_excel = sf_from_excel.data_df.itertuples()
362+
rows_in_self = self.sf.data_df.itertuples()
363+
364+
# making sure styles are the same
365+
self.assertTrue(all(self_cell.style == Styler.from_openpyxl_style(excel_cell.style, [])
366+
for row_in_excel, row_in_self in zip(rows_in_excel, rows_in_self)
367+
for excel_cell, self_cell in zip(row_in_excel[1:], row_in_self[1:])))
368+
369+
sf_from_excel.to_excel(TEST_FILENAME).save()
370+
355371
def test_read_excel_with_style_styler_objects(self):
356372
self.export_and_get_default_sheet(save=True)
357373
sf_from_excel = StyleFrame.read_excel(TEST_FILENAME, read_style=True)

0 commit comments

Comments
 (0)