Python tool for converting files and office documents to Markdown.
fix(pptx): prevent crash in PptxConverter when chart title lacks a text frame (#2194)
aorightff9015652eda
diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py b/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.pyindex 7e91ed6b..455311fe 100644--- a/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py+++ b/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py@@ -222,7 +222,7 @@ class PptxConverterWithOCR(DocumentConverter):def _convert_chart_to_markdown(self, chart):try:md = "\\n\\n### Chart"- if chart.has_title:+ if chart.has_title and chart.chart_title.text_frame is not None:md += f": {chart.chart_title.text_frame.text}"md += "\\n\\n"data = []diff --git a/packages/markitdown-ocr/tests/test_pptx_converter.py b/packages/markitdown-ocr/tests/test_pptx_converter.pyindex 724f1039..bdcc4ee1 100644--- a/packages/markitdown-ocr/tests/test_pptx_converter.py+++ b/packages/markitdown-ocr/tests/test_pptx_converter.py@@ -146,3 +146,29 @@ def test_pptx_no_ocr_service_no_tags() -> None:md = converter.convert(f, StreamInfo(extension=".pptx")).text_contentassert "*[Image OCR]" not in mdassert "[End OCR]*" not in md+++def test_pptx_ocr_chart_no_title_text_frame() -> None:+ from unittest.mock import MagicMock+ from markitdown_ocr._pptx_converter_with_ocr import PptxConverterWithOCR++ mock_chart = MagicMock()+ mock_chart.has_title = True+ mock_chart.chart_title.text_frame = None++ mock_category = MagicMock()+ mock_category.label = "Cat 1"+ mock_chart.plots = [MagicMock(categories=[mock_category])]++ mock_series = MagicMock()+ mock_series.name = "Series 1"+ mock_series.values = [10.0]+ mock_chart.series = [mock_series]++ converter = PptxConverterWithOCR()+ result = converter._convert_chart_to_markdown(mock_chart)++ assert "### Chart" in result+ assert "Cat 1" in result+ assert "Series 1" in result+ assert ":" not in resultdiff --git a/packages/markitdown/src/markitdown/converters/_pptx_converter.py b/packages/markitdown/src/markitdown/converters/_pptx_converter.pyindex 3cf206bf..aad15ff6 100644--- a/packages/markitdown/src/markitdown/converters/_pptx_converter.py+++ b/packages/markitdown/src/markitdown/converters/_pptx_converter.py@@ -310,7 +310,7 @@ class PptxConverter(DocumentConverter):def _convert_chart_to_markdown(self, chart):try:md = "\n\n### Chart"- if chart.has_title:+ if chart.has_title and chart.chart_title.text_frame is not None:md += f": {chart.chart_title.text_frame.text}"md += "\n\n"data = []diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.pyindex 148d094d..03281276 100644--- a/packages/markitdown/tests/test_module_misc.py+++ b/packages/markitdown/tests/test_module_misc.py@@ -1001,6 +1001,31 @@ def test_markitdown_llm() -> None:validate_strings(result, PPTX_TEST_STRINGS)+def test_pptx_chart_no_title_text_frame() -> None:+ from markitdown.converters._pptx_converter import PptxConverter++ mock_chart = MagicMock()+ mock_chart.has_title = True+ mock_chart.chart_title.text_frame = None++ mock_category = MagicMock()+ mock_category.label = "Cat 1"+ mock_chart.plots = [MagicMock(categories=[mock_category])]++ mock_series = MagicMock()+ mock_series.name = "Series 1"+ mock_series.values = [10.0]+ mock_chart.series = [mock_series]++ converter = PptxConverter()+ result = converter._convert_chart_to_markdown(mock_chart)++ assert "### Chart" in result+ assert "Cat 1" in result+ assert "Series 1" in result+ assert ":" not in result++def test_youtube_converter_missing_title_metadata() -> None:"""Test that YouTubeConverter converts streams with and without title metadata without raising AssertionError."""from unittest.mock import patch@@ -1291,6 +1316,7 @@ if __name__ == "__main__":test_markitdown_exiftool,test_markitdown_llm_parameters,test_markitdown_llm,+ test_pptx_chart_no_title_text_frame,test_ipynb_accepts_non_ascii,test_epub_metadata_nodevalue,]: