Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix: PDF tables re-attach columns outside partial ruling; spreadsheet source notes above the header become a quote; table weight-count errors state the expected count

hugohe3059119aba9df
diff --git a/skills/ppt-master/scripts/source_to_md/excel_to_md.py b/skills/ppt-master/scripts/source_to_md/excel_to_md.py
index 0defd41f..4221458d 100644
--- a/skills/ppt-master/scripts/source_to_md/excel_to_md.py
+++ b/skills/ppt-master/scripts/source_to_md/excel_to_md.py
@@ -193,6 +193,43 @@ def _extract_rows(
return normalized_rows, rows_truncated, cols_truncated
+def _is_caption_row(
+ row: list[Any],
+ row_index: int,
+ min_col: int,
+ merged_values: dict[tuple[int, int], Any],
+) -> bool:
+ """Return whether a leading row is a sheet title or source note, not a header.
+
+ Public datasets (World Bank, OECD, Eurostat exports) put a free-text
+ source line above the real header; it fills only the first cell or one
+ merged band, so every other column is empty or repeats the same value.
+ """
+ values = [value for value in row if not _is_empty(value)]
+ if len(values) != 1 and len({str(value) for value in values}) != 1:
+ return False
+ if len(values) == 1:
+ return len(row) > 1
+ return (row_index, min_col) in merged_values
+
+
+def _split_caption_rows(
+ rows: list[list[Any]],
+ min_row: int,
+ min_col: int,
+ merged_values: dict[tuple[int, int], Any],
+) -> tuple[list[str], list[list[Any]]]:
+ """Peel title/source rows off the top so the first table row is the header."""
+ captions: list[str] = []
+ while len(rows) > 1 and _is_caption_row(rows[0], min_row + len(captions), min_col, merged_values):
+ remaining = rows[1:]
+ if not any(len([v for v in row if not _is_empty(v)]) >= 2 for row in remaining):
+ break
+ captions.append(_format_cell_value(next(value for value in rows[0] if not _is_empty(value))))
+ rows = remaining
+ return captions, rows
+
+
def _column_alignments(rows: list[list[Any]]) -> list[str]:
if not rows:
return []
@@ -291,6 +328,10 @@ def _convert_excel(input_file: Path, out_file: Path, max_rows: int, max_cols: in
"",
])
+ captions, rows = _split_caption_rows(rows, min_row, min_col, merged_values)
+ for caption in captions:
+ lines.extend([f"> {caption}", ""])
+
if rows_truncated or cols_truncated:
limit_notes = []
if rows_truncated:
diff --git a/skills/ppt-master/scripts/source_to_md/pdf_to_md.py b/skills/ppt-master/scripts/source_to_md/pdf_to_md.py
index e7f43461..666fbb36 100644
--- a/skills/ppt-master/scripts/source_to_md/pdf_to_md.py
+++ b/skills/ppt-master/scripts/source_to_md/pdf_to_md.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import argparse
import hashlib
import json
+import math
import os
import re
import sys
@@ -1133,6 +1134,118 @@ def _add_table_candidate(
_append_table_markdown_candidate(candidates, bbox, markdown, method)
+ORPHAN_CELL_MAX_CHARS = 24
+ORPHAN_COLUMN_GAP = 12.0
+ORPHAN_ROW_COVERAGE = 0.6
+
+
+def _cluster_orphan_columns(cells: list[tuple[fitz.Rect, str, int]]) -> list[list[int]]:
+ """Group orphan text lines into columns by overlapping x-extents."""
+ ordered = sorted(range(len(cells)), key=lambda idx: cells[idx][0].x0)
+ columns: list[list[int]] = []
+ column_x1 = 0.0
+ for idx in ordered:
+ rect = cells[idx][0]
+ if columns and rect.x0 <= column_x1 + ORPHAN_COLUMN_GAP:
+ columns[-1].append(idx)
+ column_x1 = max(column_x1, rect.x1)
+ else:
+ columns.append([idx])
+ column_x1 = rect.x1
+ return columns
+
+
+def _orphan_side_columns(
+ cells: list[tuple[fitz.Rect, str, int]],
+ row_count: int,
+) -> list[list[str]] | None:
+ """Return per-row cell text for one side of a ruled table, or None."""
+ if not cells:
+ return None
+ covered_rows = {row_index for _rect, _text, row_index in cells}
+ if len(covered_rows) < max(2, math.ceil(row_count * ORPHAN_ROW_COVERAGE)):
+ return None
+ if any(len(text) > ORPHAN_CELL_MAX_CHARS for _rect, text, _row in cells):
+ return None
+
+ columns = _cluster_orphan_columns(cells)
+ table: list[list[str]] = [["" for _ in columns] for _ in range(row_count)]
+ for col_index, members in enumerate(columns):
+ for idx in sorted(members, key=lambda item: cells[item][0].x0):
+ rect, text, row_index = cells[idx]
+ current = table[row_index][col_index]
+ table[row_index][col_index] = f"{current} {text}".strip() if current else text
+ return table
+
+
+def _extend_ruled_table(
+ page: fitz.Page,
+ tab: object,
+ lines: list[tuple[fitz.Rect, str]],
+) -> tuple[fitz.Rect, str] | None:
+ """Re-attach columns that sit outside a partially ruled table's borders.
+
+ Statistical bulletins often rule only the middle columns; PyMuPDF then
+ returns a narrow table and the label and change columns fall out as loose
+ text. Text lines that share a row band with the table but lie fully to its
+ left or right are clustered into extra columns and prepended/appended.
+ """
+ try:
+ raw_rows = tab.extract() or []
+ row_rects = [fitz.Rect(row.bbox) for row in tab.rows]
+ except Exception:
+ return None
+ if len(raw_rows) < 2 or len(row_rects) != len(raw_rows):
+ return None
+
+ table_rect = fitz.Rect(tab.bbox)
+ left: list[tuple[fitz.Rect, str, int]] = []
+ right: list[tuple[fitz.Rect, str, int]] = []
+ for rect, text in lines:
+ if rect.y1 < table_rect.y0 - 2 or rect.y0 > table_rect.y1 + 2:
+ continue
+ center_y = (rect.y0 + rect.y1) / 2
+ row_index = next(
+ (
+ idx for idx, row_rect in enumerate(row_rects)
+ if row_rect.y0 - 2 <= center_y <= row_rect.y1 + 2
+ ),
+ None,
+ )
+ if row_index is None:
+ continue
+ if rect.x1 <= table_rect.x0 + 1:
+ left.append((rect, text, row_index))
+ elif rect.x0 >= table_rect.x1 - 1:
+ right.append((rect, text, row_index))
+ elif not (table_rect.x0 <= rect.x0 and rect.x1 <= table_rect.x1):
+ return None
+
+ left_cols = _orphan_side_columns(left, len(raw_rows))
+ right_cols = _orphan_side_columns(right, len(raw_rows))
+ if left_cols is None and right_cols is None:
+ return None
+
+ rows: list[list[object]] = []
+ for idx, row in enumerate(raw_rows):
+ merged: list[object] = []
+ if left_cols is not None:
+ merged.extend(left_cols[idx])
+ merged.extend(row)
+ if right_cols is not None:
+ merged.extend(right_cols[idx])
+ rows.append(merged)
+
+ markdown = _rows_to_markdown(_clean_table_rows(rows))
+ if not _is_valid_table_markdown(markdown):
+ return None
+
+ bbox = fitz.Rect(table_rect)
+ for rect, _text, _row in (left if left_cols is not None else []) + (right if right_cols is not None else []):
+ bbox |= rect
+ return bbox, markdown
+
+
def _merge_word_runs(words: list[tuple]) -> list[dict[str, object]]:
"""Group PyMuPDF words into row-level text runs."""
rows: list[list[tuple]] = []
@@ -1249,13 +1362,18 @@ def find_page_tables(
) -> tuple[list[dict[str, object]], bool]:
"""Find line-detected tables plus caption-guided text tables on one page."""
candidates: list[dict[str, object]] = []
+ lines = _extract_text_lines(page)
try:
for tab in page.find_tables():
- _add_table_candidate(candidates, tab, "lines")
+ extended = _extend_ruled_table(page, tab, lines)
+ if extended is None:
+ _add_table_candidate(candidates, tab, "lines")
+ continue
+ bbox, markdown = extended
+ _append_table_markdown_candidate(candidates, bbox, markdown, "lines+text")
except Exception:
pass
- lines = _extract_text_lines(page)
for rect, text in lines:
if not _is_table_caption(text):
continue
diff --git a/skills/ppt-master/scripts/svg_to_pptx/native_objects/table.py b/skills/ppt-master/scripts/svg_to_pptx/native_objects/table.py
index a98a108e..b0956147 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/native_objects/table.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/native_objects/table.py
@@ -574,13 +574,19 @@ def _validate_table_lengths(payload: dict[str, Any], table_rows: list[list[Any]]
column_widths = payload.get("column_widths")
if column_widths is not None:
if not isinstance(column_widths, list) or len(column_widths) != col_count:
- raise RuntimeError("Native PPTX table column_widths must match the resolved column count")
+ raise RuntimeError(
+ "Native PPTX table column_widths must match the resolved column count "
+ f"(expected {col_count} entries)"
+ )
_table_weights(column_widths, "column_widths")
row_heights = payload.get("row_heights")
if row_heights is not None:
if not isinstance(row_heights, list) or len(row_heights) != len(table_rows):
- raise RuntimeError("Native PPTX table row_heights must match the resolved row count")
+ raise RuntimeError(
+ "Native PPTX table row_heights must match the resolved row count "
+ f"(expected {len(table_rows)} entries, header rows included)"
+ )
_table_weights(row_heights, "row_heights")
return col_count
diff --git a/skills/ppt-master/scripts/tests/test_source_table_recovery.py b/skills/ppt-master/scripts/tests/test_source_table_recovery.py
new file mode 100644
index 00000000..1d877b51
--- /dev/null
+++ b/skills/ppt-master/scripts/tests/test_source_table_recovery.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+"""Regression tests for table recovery in the PDF and spreadsheet converters.
+
+Bulletin PDFs that rule only the middle columns must still yield a full table,
+and a spreadsheet whose first row is a source note must keep the real header.
+"""
+
+from __future__ import annotations
+
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+SCRIPTS_DIR = Path(__file__).resolve().parents[1]
+if str(SCRIPTS_DIR) not in sys.path:
+ sys.path.insert(0, str(SCRIPTS_DIR))
+BACKEND_DIR = SCRIPTS_DIR / "source_to_md"
+if str(BACKEND_DIR) not in sys.path:
+ sys.path.insert(0, str(BACKEND_DIR))
+
+import fitz # noqa: E402
+import excel_to_md # noqa: E402
+import pdf_to_md # noqa: E402
+
+
+def _partially_ruled_table_page() -> fitz.Page:
+ """Draw a 4-column table whose ruling lines enclose only columns 2 and 3."""
+ doc = fitz.open()
+ page = doc.new_page(width=600, height=400)
+ rows = [
+ ("Item", "Unit", "2024", "Change"),
+ ("Passengers", "million", "4312", "11.9"),
+ ("National", "million", "4085", "10.9"),
+ ("Other", "million", "227", "34.0"),
+ ("Turnover", "bn pkm", "15799", "7.3"),
+ ]
+ top = 100
+ step = 24
+ for row_index, row in enumerate(rows):
+ y = top + row_index * step + 16
+ for col_index, (x, text) in enumerate(zip((60, 240, 340, 460), row)):
+ page.insert_text((x, y), text, fontsize=10)
+ shape = page.new_shape()
+ x0, x1 = 230, 430
+ for row_index in range(len(rows) + 1):
+ y = top + row_index * step
+ shape.draw_line((x0, y), (x1, y))
+ for x in (x0, 330, x1):
+ shape.draw_line((x, top), (x, top + len(rows) * step))
+ shape.finish(color=(0, 0, 0), width=0.8)
+ shape.commit()
+ return page
+
+
+class RuledTableRecoveryTests(unittest.TestCase):
+ def test_columns_outside_the_ruling_are_reattached(self) -> None:
+ page = _partially_ruled_table_page()
+ candidates, _continues = pdf_to_md.find_page_tables(page)
+ self.assertEqual(len(candidates), 1)
+ markdown = str(candidates[0]["content"])
+ lines = markdown.splitlines()
+ self.assertEqual(lines[0], "|Item|Unit|2024|Change|")
+ self.assertIn("|Passengers|million|4312|11.9|", lines)
+ self.assertIn("|Turnover|bn pkm|15799|7.3|", lines)
+ self.assertEqual(candidates[0]["method"], "lines+text")
+ bbox = candidates[0]["bbox"]
+ self.assertLess(bbox.x0, 100)
+ self.assertGreater(bbox.x1, 460)
+
+ def test_prose_beside_a_ruled_table_is_left_alone(self) -> None:
+ page = _partially_ruled_table_page()
+ for row_index in range(5):
+ page.insert_text(
+ (10, 100 + row_index * 24 + 16),
+ "A running sentence sits beside the table here",
+ fontsize=8,
+ )
+ candidates, _continues = pdf_to_md.find_page_tables(page)
+ self.assertEqual(len(candidates), 1)
+ lines = str(candidates[0]["content"]).splitlines()
+ self.assertEqual(lines[0], "|Unit|2024|Change|")
+ self.assertNotIn("sentence", str(candidates[0]["content"]))
+ self.assertGreaterEqual(candidates[0]["bbox"].x0, 230)
+
+
+class SpreadsheetCaptionRowTests(unittest.TestCase):
+ def _convert(self, build) -> str:
+ from openpyxl import Workbook
+
+ workbook = Workbook()
+ sheet = workbook.active
+ sheet.title = "data"
+ build(sheet)
+ with tempfile.TemporaryDirectory() as tmp:
+ source = Path(tmp) / "book.xlsx"
+ workbook.save(source)
+ return excel_to_md.convert_to_markdown(str(source), str(Path(tmp) / "book.md"))
+
+ def test_source_note_in_a1_becomes_a_quote_not_the_header(self) -> None:
+ def build(sheet) -> None:
+ sheet.append(["Source: World Bank, IS.RRS.TOTL.KM, downloaded 2026-09-12"])
+ sheet.append(["Country", "Code", 2000, 2001])
+ sheet.append(["China", "CHN", 58656, 59079])
+
+ markdown = self._convert(build)
+ self.assertIn("> Source: World Bank, IS.RRS.TOTL.KM, downloaded 2026-09-12", markdown)
+ self.assertIn("| Country | Code | 2000 | 2001 |", markdown)
+ self.assertNotIn("| Source: World Bank", markdown)
+
+ def test_merged_title_band_is_also_peeled(self) -> None:
+ def build(sheet) -> None:
+ sheet.append(["Quarterly revenue"])
+ sheet.merge_cells("A1:C1")
+ sheet.append(["Quarter", "Revenue", "Growth"])
+ sheet.append(["Q1", 10, 0.1])
+
+ markdown = self._convert(build)
+ self.assertIn("> Quarterly revenue", markdown)
+ self.assertIn("| Quarter | Revenue | Growth |", markdown)
+
+ def test_single_column_sheets_and_real_headers_are_untouched(self) -> None:
+ def build(sheet) -> None:
+ sheet.append(["Name", "Score"])
+ sheet.append(["Ann", 3])
+
+ markdown = self._convert(build)
+ self.assertIn("| Name | Score |", markdown)
+ self.assertNotIn("> Name", markdown)
+
+ def build_single(sheet) -> None:
+ sheet.append(["Memo"])
+ sheet.append(["Only one column"])
+
+ markdown = self._convert(build_single)
+ self.assertIn("| Memo |", markdown)
+ self.assertNotIn("> Memo", markdown)