Python tool for converting files and office documents to Markdown.
fix(pptx): do not emit a Notes heading for a slide with no notes (#2427)
kevin481f22a9a9a5
diff --git a/packages/markitdown/src/markitdown/converters/_pptx_converter.py b/packages/markitdown/src/markitdown/converters/_pptx_converter.pyindex 3dafba57..e11ff5f7 100644--- a/packages/markitdown/src/markitdown/converters/_pptx_converter.py+++ b/packages/markitdown/src/markitdown/converters/_pptx_converter.py@@ -209,11 +209,14 @@ class PptxConverter(DocumentConverter):md_content = md_content.strip()if slide.has_notes_slide:- md_content += "\n\n### Notes:\n"+ # PowerPoint attaches a notes slide to a slide whose notes pane+ # has merely been opened, so having one says nothing about there+ # being notes to read. Only head a section that has content.notes_frame = slide.notes_slide.notes_text_frame- if notes_frame is not None:- md_content += notes_frame.text or ""- md_content = md_content.strip()+ notes_text = (notes_frame.text or "") if notes_frame is not None else ""+ if notes_text.strip():+ md_content += "\n\n### Notes:\n" + notes_text+ md_content = md_content.strip()return DocumentConverterResult(markdown=md_content.strip())diff --git a/packages/markitdown/tests/test_pptx_notes.py b/packages/markitdown/tests/test_pptx_notes.pynew file mode 100644index 00000000..7289ffa7--- /dev/null+++ b/packages/markitdown/tests/test_pptx_notes.py@@ -0,0 +1,58 @@+#!/usr/bin/env python3 -m pytest+"""A slide whose notes slide carries no text must not get a "Notes:" heading."""++import io++import pptx++from markitdown import MarkItDown, StreamInfo+++def _build_pptx(notes: str | None) -> io.BytesIO:+ """Build a one-slide deck in memory, optionally attaching a notes slide."""+ prs = pptx.Presentation()+ slide = prs.slides.add_slide(prs.slide_layouts[5])+ slide.shapes.title.text = "Slide title"+ if notes is not None:+ # Touching notes_slide creates the part, which is what PowerPoint does+ # for a deck whose notes pane has been opened.+ slide.notes_slide.notes_text_frame.text = notes+ buf = io.BytesIO()+ prs.save(buf)+ buf.seek(0)+ return buf+++def _convert(notes: str | None) -> str:+ return (+ MarkItDown()+ .convert_stream(_build_pptx(notes), stream_info=StreamInfo(extension=".pptx"))+ .markdown+ )+++def test_empty_notes_slide_produces_no_notes_heading() -> None:+ markdown = _convert("")++ assert "Slide title" in markdown+ assert "### Notes:" not in markdown+++def test_whitespace_only_notes_produce_no_notes_heading() -> None:+ markdown = _convert(" \n\n ")++ assert "Slide title" in markdown+ assert "### Notes:" not in markdown+++def test_slide_without_a_notes_slide_produces_no_notes_heading() -> None:+ markdown = _convert(None)++ assert "Slide title" in markdown+ assert "### Notes:" not in markdown+++def test_notes_with_text_are_still_emitted() -> None:+ markdown = _convert("Remember to mention the budget.")++ assert "### Notes:\nRemember to mention the budget." in markdown