fix(documents): skip chart-only sheets when reading XLSX files
This commit is contained in:
1 parent
6330b748de
commit
a2f9fae8fe
2 files changed
+34
-2
No files matched your search
@@ -251,8 +251,8 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource:
|
||||
extracted_line = 0
|
||||
wrote_document_content = False
|
||||
try:
|
||||
for sheet_name in workbook.sheetnames:
|
||||
worksheet = workbook[sheet_name]
|
||||
for worksheet in workbook.worksheets:
|
||||
sheet_name = worksheet.title
|
||||
# Producer-supplied dimensions can omit cells that are present in the XML.
|
||||
worksheet.reset_dimensions()
|
||||
wrote_header = False
|
||||
|
||||
@@ -163,6 +163,38 @@ class TestExtractText:
|
||||
assert "Widget" in result
|
||||
assert "9.99" in result
|
||||
|
||||
@pytest.mark.parametrize("chart_index", [0, 1, 2])
|
||||
def test_extract_text_xlsx_with_chartsheet(self, tmp_path: Path, chart_index: int):
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.chart import BarChart, Reference
|
||||
|
||||
xlsx_file = tmp_path / "report.xlsx"
|
||||
workbook = Workbook()
|
||||
first_sheet = workbook.active
|
||||
first_sheet.title = "Sales"
|
||||
first_sheet.append(["Revenue", 42])
|
||||
second_sheet = workbook.create_sheet("Notes")
|
||||
second_sheet.append(["Forecast", 50])
|
||||
chart = BarChart()
|
||||
chart.add_data(Reference(first_sheet, min_col=2, min_row=1))
|
||||
workbook.create_chartsheet("Dashboard", index=chart_index).add_chart(chart)
|
||||
workbook.save(xlsx_file)
|
||||
workbook.close()
|
||||
|
||||
assert extract_text(xlsx_file) == (
|
||||
"--- Sheet: Sales ---\nRevenue\t42\n\n--- Sheet: Notes ---\nForecast\t50"
|
||||
)
|
||||
source = open_document_line_source(xlsx_file)
|
||||
assert source is not None
|
||||
assert [
|
||||
(line.text, line.extracted_line, line.locator)
|
||||
for line in source.lines
|
||||
if line.searchable
|
||||
] == [
|
||||
("Revenue\t42", 2, "sheet='Sales',row=1"),
|
||||
("Forecast\t50", 5, "sheet='Notes',row=1"),
|
||||
]
|
||||
|
||||
def test_extract_text_xlsx_empty_sheet(self, tmp_path: Path):
|
||||
"""Test extracting text from an .xlsx file with empty sheets."""
|
||||
from openpyxl import Workbook
|
||||
|
||||
Reference in new issue
Block a user