Python tool for converting files and office documents to Markdown.
fix(pptx): chart_title.text_frame is never None -- use has_text_frame (#2385)
kapil9713904459ed01155f
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 455311fe..5988c165 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,11 @@ class PptxConverterWithOCR(DocumentConverter):def _convert_chart_to_markdown(self, chart):try:md = "\\n\\n### Chart"- if chart.has_title and chart.chart_title.text_frame is not None:+ # ChartTitle.text_frame is documented as destructive -- it creates+ # a text frame if one isn't already present, so it never returns+ # None. has_text_frame is the property that actually reflects+ # whether a text frame exists.+ if chart.has_title and chart.chart_title.has_text_frame: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 bdcc4ee1..989724f3 100644--- a/packages/markitdown-ocr/tests/test_pptx_converter.py+++ b/packages/markitdown-ocr/tests/test_pptx_converter.py@@ -152,9 +152,12 @@ def test_pptx_ocr_chart_no_title_text_frame() -> None:from unittest.mock import MagicMockfrom markitdown_ocr._pptx_converter_with_ocr import PptxConverterWithOCR+ # python-pptx's ChartTitle.text_frame is destructive -- it creates a text+ # frame if one isn't already present, so it never returns None.+ # has_text_frame is the property that actually reflects presence/absence.mock_chart = MagicMock()mock_chart.has_title = True- mock_chart.chart_title.text_frame = None+ mock_chart.chart_title.has_text_frame = Falsemock_category = MagicMock()mock_category.label = "Cat 1"@@ -172,3 +175,27 @@ def test_pptx_ocr_chart_no_title_text_frame() -> None:assert "Cat 1" in resultassert "Series 1" in resultassert ":" not in result+++def test_pptx_ocr_chart_with_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.has_text_frame = True+ mock_chart.chart_title.text_frame.text = "Revenue"++ 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: Revenue" in resultdiff --git a/packages/markitdown/src/markitdown/converters/_pptx_converter.py b/packages/markitdown/src/markitdown/converters/_pptx_converter.pyindex aad15ff6..3dafba57 100644--- a/packages/markitdown/src/markitdown/converters/_pptx_converter.py+++ b/packages/markitdown/src/markitdown/converters/_pptx_converter.py@@ -310,7 +310,11 @@ class PptxConverter(DocumentConverter):def _convert_chart_to_markdown(self, chart):try:md = "\n\n### Chart"- if chart.has_title and chart.chart_title.text_frame is not None:+ # ChartTitle.text_frame is documented as destructive -- it creates+ # a text frame if one isn't already present, so it never returns+ # None. has_text_frame is the property that actually reflects+ # whether a text frame exists.+ if chart.has_title and chart.chart_title.has_text_frame: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 1effa4cd..f607a4c7 100644--- a/packages/markitdown/tests/test_module_misc.py+++ b/packages/markitdown/tests/test_module_misc.py@@ -1281,9 +1281,14 @@ def test_markitdown_llm() -> None:def test_pptx_chart_no_title_text_frame() -> None:from markitdown.converters._pptx_converter import PptxConverter+ # python-pptx's ChartTitle.text_frame is destructive -- it creates a text+ # frame if one isn't already present, so it never returns None.+ # has_text_frame is the property that actually reflects presence/absence,+ # which is what the has_title=True-but-no-text-frame case looks like+ # against the real library.mock_chart = MagicMock()mock_chart.has_title = True- mock_chart.chart_title.text_frame = None+ mock_chart.chart_title.has_text_frame = Falsemock_category = MagicMock()mock_category.label = "Cat 1"@@ -1303,6 +1308,29 @@ def test_pptx_chart_no_title_text_frame() -> None:assert ":" not in result+def test_pptx_chart_with_title_text_frame() -> None:+ from markitdown.converters._pptx_converter import PptxConverter++ mock_chart = MagicMock()+ mock_chart.has_title = True+ mock_chart.chart_title.has_text_frame = True+ mock_chart.chart_title.text_frame.text = "Revenue"++ 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: Revenue" 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@@ -1650,6 +1678,7 @@ if __name__ == "__main__":test_markitdown_llm_parameters,test_markitdown_llm,test_pptx_chart_no_title_text_frame,+ test_pptx_chart_with_title_text_frame,test_ipynb_accepts_non_ascii,test_epub_metadata_nodevalue,test_exiftool_metadata_with_nonexistent_binary,