File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -510,6 +510,10 @@ def read_excel(
510
510
if engine_kwargs is None :
511
511
engine_kwargs = {}
512
512
513
+ # set to false so cells have a comment attribute
514
+ if notes is not None and engine == "openpyxl" :
515
+ engine_kwargs = {"read_only" : False }
516
+
513
517
if not isinstance (io , ExcelFile ):
514
518
should_close = True
515
519
io = ExcelFile (
Original file line number Diff line number Diff line change 37
37
WriteExcelBuffer ,
38
38
)
39
39
40
+ from pandas .core .frame import DataFrame
41
+
40
42
41
43
class OpenpyxlWriter (ExcelWriter ):
42
44
_engine = "openpyxl"
@@ -626,11 +628,43 @@ def _convert_cell(self, cell) -> Scalar:
626
628
return cell .value
627
629
628
630
def get_sheet_data (
629
- self , sheet , file_rows_needed : int | None = None
631
+ self , sheet , file_rows_needed : int | None = None , notes : DataFrame | None = None
630
632
) -> list [list [Scalar ]]:
631
633
if self .book .read_only :
632
634
sheet .reset_dimensions ()
633
635
636
+ if notes is not None :
637
+ data_notes = []
638
+ for row in sheet .rows :
639
+ row_notes = [
640
+ cell .comment .content if cell .comment else "" for cell in row
641
+ ]
642
+ data_notes .append (row_notes )
643
+
644
+ # trimming trailing empty rows and columns
645
+ while data_notes and all (cell == "" for cell in data_notes [0 ]):
646
+ data_notes .pop (0 )
647
+
648
+ while data_notes and all (cell == "" for cell in data_notes [- 1 ]):
649
+ data_notes .pop ()
650
+
651
+ while data_notes and all (
652
+ cell == "" for cell in [row [0 ] for row in data_notes ]
653
+ ):
654
+ for row in data_notes :
655
+ row .pop (0 )
656
+
657
+ while data_notes and all (
658
+ cell == "" for cell in [row [- 1 ] for row in data_notes ]
659
+ ):
660
+ for row in data_notes :
661
+ row .pop ()
662
+
663
+ notes_df = DataFrame (data_notes )
664
+
665
+ for col in notes_df .columns :
666
+ notes [col ] = notes_df [col ]
667
+
634
668
data : list [list [Scalar ]] = []
635
669
last_row_with_data = - 1
636
670
for row_number , row in enumerate (sheet .rows ):
You can’t perform that action at this time.
0 commit comments