Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 1c8937c

Browse files
committed
[REF]: copy_spanning_text
1 parent ff9a501 commit 1c8937c

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

camelot/core.py

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -664,35 +664,72 @@ def set_span(self):
664664
return self
665665

666666
def copy_spanning_text(self, copy_text=None):
667-
"""Copies over text in empty spanning cells.
667+
"""
668+
Copies over text in empty spanning cells.
668669
669670
Parameters
670671
----------
671-
copy_text : list, optional (default: None)
672-
{'h', 'v'}
673-
Select one or more strings from above and pass them as a list
674-
to specify the direction in which text should be copied over
675-
when a cell spans multiple rows or columns.
672+
copy_text : list of str, optional (default: None)
673+
Select one or more of the following strings: {'h', 'v'} to specify
674+
the direction in which text should be copied over when a cell spans
675+
multiple rows or columns.
676676
677677
Returns
678678
-------
679-
t : camelot.core.Table
679+
camelot.core.Table
680+
The updated table with copied text in spanning cells.
680681
"""
681-
for f in copy_text:
682-
if f == "h":
683-
for i in range(len(self.cells)):
684-
for j in range(len(self.cells[i])):
685-
if self.cells[i][j].text.strip() == "":
686-
if self.cells[i][j].hspan and not self.cells[i][j].left:
687-
self.cells[i][j].text = self.cells[i][j - 1].text
688-
elif f == "v":
689-
for i in range(len(self.cells)):
690-
for j in range(len(self.cells[i])):
691-
if self.cells[i][j].text.strip() == "":
692-
if self.cells[i][j].vspan and not self.cells[i][j].top:
693-
self.cells[i][j].text = self.cells[i - 1][j].text
682+
if copy_text is None:
683+
return self
684+
685+
for direction in copy_text:
686+
if direction == "h":
687+
self._copy_horizontal_text()
688+
elif direction == "v":
689+
self._copy_vertical_text()
690+
694691
return self
695692

693+
def _copy_horizontal_text(self):
694+
"""
695+
Copies text horizontally in empty spanning cells.
696+
697+
This method iterates through the cells and fills empty cells that span
698+
horizontally with the text from the left adjacent cell.
699+
700+
Returns
701+
-------
702+
None
703+
"""
704+
for i in range(len(self.cells)):
705+
for j in range(len(self.cells[i])):
706+
if (
707+
self.cells[i][j].text.strip() == ""
708+
and self.cells[i][j].hspan
709+
and not self.cells[i][j].left
710+
):
711+
self.cells[i][j].text = self.cells[i][j - 1].text
712+
713+
def _copy_vertical_text(self):
714+
"""
715+
Copies text vertically in empty spanning cells.
716+
717+
This method iterates through the cells and fills empty cells that span
718+
vertically with the text from the top adjacent cell.
719+
720+
Returns
721+
-------
722+
None
723+
"""
724+
for i in range(len(self.cells)):
725+
for j in range(len(self.cells[i])):
726+
if (
727+
self.cells[i][j].text.strip() == ""
728+
and self.cells[i][j].vspan
729+
and not self.cells[i][j].top
730+
):
731+
self.cells[i][j].text = self.cells[i - 1][j].text
732+
696733
def to_csv(self, path, **kwargs):
697734
"""Write Table(s) to a comma-separated values (csv) file.
698735

0 commit comments

Comments
 (0)