fix(documents): preserve empty PPTX table columns
This commit is contained in:
1 parent
328e933d36
commit
db804ef94a
2 files changed
+36
-3
No files matched your search
@@ -574,9 +574,8 @@ def _collect_pptx_shape_text(shape: Any, out: list[str]) -> None:
|
||||
if getattr(shape, "has_table", False):
|
||||
for row in shape.table.rows:
|
||||
cells = [cell.text.strip() for cell in row.cells]
|
||||
line = "\t".join(cell for cell in cells if cell)
|
||||
if line:
|
||||
out.append(line)
|
||||
if any(cells):
|
||||
out.append("\t".join(cells))
|
||||
return
|
||||
|
||||
text = getattr(shape, "text", "")
|
||||
|
||||
@@ -385,6 +385,40 @@ class TestExtractText:
|
||||
assert "Alice" in result
|
||||
assert "Bob" in result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cells", "expected"),
|
||||
[
|
||||
(("Alice", "", "100"), "Alice\t\t100"),
|
||||
(("", "10", "100"), "\t10\t100"),
|
||||
(("Alice", "10", ""), "Alice\t10\t"),
|
||||
(("", " ", ""), None),
|
||||
],
|
||||
)
|
||||
def test_extract_text_pptx_table_preserves_empty_columns(
|
||||
self, tmp_path: Path, cells: tuple[str, ...], expected: str | None
|
||||
):
|
||||
"""Blank cells keep values aligned with their table headers."""
|
||||
from pptx import Presentation
|
||||
from pptx.util import Inches
|
||||
|
||||
pptx_file = tmp_path / "empty-columns.pptx"
|
||||
prs = Presentation()
|
||||
slide = prs.slides.add_slide(prs.slide_layouts[6])
|
||||
table = slide.shapes.add_table(
|
||||
2, 3, Inches(1), Inches(1), Inches(4), Inches(1)
|
||||
).table
|
||||
for column, text in enumerate(("Name", "Q1", "Q2")):
|
||||
table.cell(0, column).text = text
|
||||
for column, text in enumerate(cells):
|
||||
table.cell(1, column).text = text
|
||||
prs.save(pptx_file)
|
||||
|
||||
result = extract_text(pptx_file)
|
||||
expected_lines = ["--- Slide 1 ---", "Name\tQ1\tQ2"]
|
||||
if expected is not None:
|
||||
expected_lines.append(expected)
|
||||
assert result == "\n".join(expected_lines)
|
||||
|
||||
def test_extract_text_pptx_grouped_shapes(self, tmp_path: Path):
|
||||
"""Text inside grouped shapes must be extracted recursively."""
|
||||
from pptx import Presentation
|
||||
|
||||
Reference in new issue
Block a user