Skip to content

Python tool for converting files and office documents to Markdown.

fix: PptxConverter tolerates None shape.text and notes text (#2059)

Alvin Tangcfe54d24cc45
diff --git a/packages/markitdown/src/markitdown/converters/_pptx_converter.py b/packages/markitdown/src/markitdown/converters/_pptx_converter.py
index 41ee5c04..3cf206bf 100644
--- a/packages/markitdown/src/markitdown/converters/_pptx_converter.py
+++ b/packages/markitdown/src/markitdown/converters/_pptx_converter.py
@@ -178,10 +178,11 @@ class PptxConverter(DocumentConverter):
# Text areas
elif shape.has_text_frame:
+ text = shape.text or ""
if shape == title:
- md_content += "# " + shape.text.lstrip() + "\n"
+ md_content += "# " + text.lstrip() + "\n"
else:
- md_content += shape.text + "\n"
+ md_content += text + "\n"
# Group Shapes
if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.GROUP:
@@ -211,7 +212,7 @@ class PptxConverter(DocumentConverter):
md_content += "\n\n### Notes:\n"
notes_frame = slide.notes_slide.notes_text_frame
if notes_frame is not None:
- md_content += notes_frame.text
+ md_content += notes_frame.text or ""
md_content = md_content.strip()
return DocumentConverterResult(markdown=md_content.strip())
diff --git a/packages/markitdown/tests/test_pptx_none_text.py b/packages/markitdown/tests/test_pptx_none_text.py
new file mode 100644
index 00000000..be5d5cbe
--- /dev/null
+++ b/packages/markitdown/tests/test_pptx_none_text.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3 -m pytest
+"""Regression test for #1808: PptxConverter must not crash when ``shape.text``
+or ``notes_text_frame.text`` returns ``None`` (e.g. an ``<a:r>`` run with no
+``<a:t>`` child, or certain third-party-generated decks)."""
+
+import io
+import os
+
+import pptx
+import pptx.shapes.autoshape
+import pptx.slide
+
+from markitdown import MarkItDown
+
+TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files")
+PPTX_FIXTURE = os.path.join(TEST_FILES_DIR, "test.pptx")
+
+
+def _build_pptx_with_notes() -> io.BytesIO:
+ """Build a minimal pptx with a notes slide attached, in memory."""
+ prs = pptx.Presentation()
+ blank = prs.slide_layouts[6]
+ slide = prs.slides.add_slide(blank)
+ slide.notes_slide.notes_text_frame.text = "some notes"
+ buf = io.BytesIO()
+ prs.save(buf)
+ buf.seek(0)
+ return buf
+
+
+def test_pptx_converter_handles_none_shape_text(monkeypatch):
+ """``shape.text`` returning None must not blow up the title / body paths."""
+ monkeypatch.setattr(
+ pptx.shapes.autoshape.Shape,
+ "text",
+ property(lambda self: None),
+ raising=True,
+ )
+
+ result = MarkItDown().convert(PPTX_FIXTURE)
+ assert result.markdown is not None
+
+
+def test_pptx_converter_handles_none_notes_text(monkeypatch):
+ """``notes_text_frame.text`` returning None must not blow up the notes path."""
+ real_notes = pptx.slide.NotesSlide.notes_text_frame
+
+ class _NoneText:
+ text = None
+
+ def fake_notes(self):
+ frame = real_notes.fget(self)
+ if frame is None:
+ return None
+ return _NoneText()
+
+ monkeypatch.setattr(
+ pptx.slide.NotesSlide,
+ "notes_text_frame",
+ property(fake_notes),
+ raising=True,
+ )
+
+ buf = _build_pptx_with_notes()
+ result = MarkItDown().convert_stream(buf, file_extension=".pptx")
+ assert result.markdown is not None