mirror of
https://github.com/DS4SD/docling.git
synced 2025-07-31 22:44:27 +00:00
reformatted the code
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
This commit is contained in:
parent
f837105a09
commit
d23aea981d
@ -177,8 +177,8 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
"""
|
"""
|
||||||
Find all compact rectangular data tables in a sheet.
|
Find all compact rectangular data tables in a sheet.
|
||||||
"""
|
"""
|
||||||
#_log.info("find_data_tables")
|
# _log.info("find_data_tables")
|
||||||
|
|
||||||
tables = [] # List to store found tables
|
tables = [] # List to store found tables
|
||||||
visited: set[Tuple[int, int]] = set() # Track already visited cells
|
visited: set[Tuple[int, int]] = set() # Track already visited cells
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
tables.append(table_bounds)
|
tables.append(table_bounds)
|
||||||
|
|
||||||
return tables
|
return tables
|
||||||
|
|
||||||
def _find_table_bounds(
|
def _find_table_bounds(
|
||||||
self,
|
self,
|
||||||
sheet: Worksheet,
|
sheet: Worksheet,
|
||||||
@ -217,25 +217,29 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
|
|
||||||
max_row = self._find_table_bottom(sheet, start_row, start_col)
|
max_row = self._find_table_bottom(sheet, start_row, start_col)
|
||||||
max_col = self._find_table_right(sheet, start_row, start_col)
|
max_col = self._find_table_right(sheet, start_row, start_col)
|
||||||
|
|
||||||
# Collect the data within the bounds
|
# Collect the data within the bounds
|
||||||
data = []
|
data = []
|
||||||
visited_cells = set()
|
visited_cells = set()
|
||||||
for ri in range(start_row, max_row + 1):
|
for ri in range(start_row, max_row + 1):
|
||||||
for rj in range(start_col, max_col + 1):
|
for rj in range(start_col, max_col + 1):
|
||||||
|
|
||||||
cell = sheet.cell(row=ri + 1, column=rj + 1) # 1-based indexing
|
cell = sheet.cell(row=ri + 1, column=rj + 1) # 1-based indexing
|
||||||
|
|
||||||
# Check if the cell belongs to a merged range
|
# Check if the cell belongs to a merged range
|
||||||
row_span = 1
|
row_span = 1
|
||||||
col_span = 1
|
col_span = 1
|
||||||
|
|
||||||
#_log.info(sheet.merged_cells.ranges)
|
# _log.info(sheet.merged_cells.ranges)
|
||||||
for merged_range in sheet.merged_cells.ranges:
|
for merged_range in sheet.merged_cells.ranges:
|
||||||
|
|
||||||
if merged_range.min_row<=ri+1 and ri+1<=merged_range.max_row and \
|
if (
|
||||||
merged_range.min_col<=rj+1 and rj+1<=merged_range.max_col:
|
merged_range.min_row <= ri + 1
|
||||||
|
and ri + 1 <= merged_range.max_row
|
||||||
|
and merged_range.min_col <= rj + 1
|
||||||
|
and rj + 1 <= merged_range.max_col
|
||||||
|
):
|
||||||
|
|
||||||
row_span = merged_range.max_row - merged_range.min_row + 1
|
row_span = merged_range.max_row - merged_range.min_row + 1
|
||||||
col_span = merged_range.max_col - merged_range.min_col + 1
|
col_span = merged_range.max_col - merged_range.min_col + 1
|
||||||
break
|
break
|
||||||
@ -243,15 +247,15 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
if (ri, rj) not in visited_cells:
|
if (ri, rj) not in visited_cells:
|
||||||
data.append(
|
data.append(
|
||||||
ExcelCell(
|
ExcelCell(
|
||||||
row = ri - start_row,
|
row=ri - start_row,
|
||||||
col = rj - start_col,
|
col=rj - start_col,
|
||||||
text=str(cell.value),
|
text=str(cell.value),
|
||||||
row_span=row_span,
|
row_span=row_span,
|
||||||
col_span=col_span,
|
col_span=col_span,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# _log.info(f"cell: {ri}, {rj} -> {ri - start_row}, {rj - start_col}, {row_span}, {col_span}: {str(cell.value)}")
|
# _log.info(f"cell: {ri}, {rj} -> {ri - start_row}, {rj - start_col}, {row_span}, {col_span}: {str(cell.value)}")
|
||||||
|
|
||||||
# Mark all cells in the span as visited
|
# Mark all cells in the span as visited
|
||||||
for span_row in range(ri, ri + row_span):
|
for span_row in range(ri, ri + row_span):
|
||||||
for span_col in range(rj, rj + col_span):
|
for span_col in range(rj, rj + col_span):
|
||||||
@ -266,41 +270,41 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
visited_cells,
|
visited_cells,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _find_table_bottom(self, sheet: Worksheet, start_row:int, start_col:int):
|
def _find_table_bottom(self, sheet: Worksheet, start_row: int, start_col: int):
|
||||||
"""Function to find the bottom boundary of the table"""
|
"""Function to find the bottom boundary of the table"""
|
||||||
|
|
||||||
max_row = start_row
|
max_row = start_row
|
||||||
|
|
||||||
while max_row < sheet.max_row - 1:
|
while max_row < sheet.max_row - 1:
|
||||||
# Get the cell value or check if it is part of a merged cell
|
# Get the cell value or check if it is part of a merged cell
|
||||||
cell = sheet.cell(row=max_row + 2, column=start_col + 1)
|
cell = sheet.cell(row=max_row + 2, column=start_col + 1)
|
||||||
|
|
||||||
# Check if the cell is part of a merged range
|
# Check if the cell is part of a merged range
|
||||||
merged_range = next(
|
merged_range = next(
|
||||||
(mr for mr in sheet.merged_cells.ranges if cell.coordinate in mr),
|
(mr for mr in sheet.merged_cells.ranges if cell.coordinate in mr),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
|
||||||
if cell.value is None and not merged_range:
|
if cell.value is None and not merged_range:
|
||||||
break # Stop if the cell is empty and not merged
|
break # Stop if the cell is empty and not merged
|
||||||
|
|
||||||
# Expand max_row to include the merged range if applicable
|
# Expand max_row to include the merged range if applicable
|
||||||
if merged_range:
|
if merged_range:
|
||||||
max_row = max(max_row, merged_range.max_row-1)
|
max_row = max(max_row, merged_range.max_row - 1)
|
||||||
else:
|
else:
|
||||||
max_row += 1
|
max_row += 1
|
||||||
|
|
||||||
return max_row
|
return max_row
|
||||||
|
|
||||||
def _find_table_right(self, sheet: Worksheet, start_row:int, start_col:int):
|
def _find_table_right(self, sheet: Worksheet, start_row: int, start_col: int):
|
||||||
"""Function to find the right boundary of the table"""
|
"""Function to find the right boundary of the table"""
|
||||||
|
|
||||||
max_col = start_col
|
max_col = start_col
|
||||||
|
|
||||||
while max_col < sheet.max_column - 1:
|
while max_col < sheet.max_column - 1:
|
||||||
# Get the cell value or check if it is part of a merged cell
|
# Get the cell value or check if it is part of a merged cell
|
||||||
cell = sheet.cell(row=start_row + 1, column=max_col + 2)
|
cell = sheet.cell(row=start_row + 1, column=max_col + 2)
|
||||||
|
|
||||||
# Check if the cell is part of a merged range
|
# Check if the cell is part of a merged range
|
||||||
merged_range = next(
|
merged_range = next(
|
||||||
(mr for mr in sheet.merged_cells.ranges if cell.coordinate in mr),
|
(mr for mr in sheet.merged_cells.ranges if cell.coordinate in mr),
|
||||||
@ -309,16 +313,15 @@ class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
|
|
||||||
if cell.value is None and not merged_range:
|
if cell.value is None and not merged_range:
|
||||||
break # Stop if the cell is empty and not merged
|
break # Stop if the cell is empty and not merged
|
||||||
|
|
||||||
# Expand max_col to include the merged range if applicable
|
# Expand max_col to include the merged range if applicable
|
||||||
if merged_range:
|
if merged_range:
|
||||||
max_col = max(max_col, merged_range.max_col-1)
|
max_col = max(max_col, merged_range.max_col - 1)
|
||||||
else:
|
else:
|
||||||
max_col += 1
|
max_col += 1
|
||||||
|
|
||||||
return max_col
|
return max_col
|
||||||
|
|
||||||
|
|
||||||
def _find_images_in_sheet(
|
def _find_images_in_sheet(
|
||||||
self, doc: DoclingDocument, sheet: Worksheet
|
self, doc: DoclingDocument, sheet: Worksheet
|
||||||
) -> DoclingDocument:
|
) -> DoclingDocument:
|
||||||
|
Loading…
Reference in New Issue
Block a user