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_to_md): keep body text that repeats a running header or footer
hugohe351c2979a1003
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.pyindex ecb6dd77..da5cccb9 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@@ -46,6 +46,7 @@ FONT_H2_SIZE = 18FONT_H3_SIZE = 14HEADER_FOOTER_SAMPLE_LIMIT = 40HEADER_FOOTER_EDGE_SAMPLE_SIZE = 20+HEADER_FOOTER_BAND_RATIO = 0.15CONTROL_CHARS_RE = re.compile(r'[\x00-\x08\x0b\x0c\x0e-\x1f]')@@ -299,16 +300,29 @@ def remove_page_footer(text: str) -> str:return text.rstrip()-def detect_headers_footers(doc: fitz.Document, threshold_ratio: float = 0.6) -> set[str]:+def _header_footer_band(bbox: fitz.Rect, page_rect: fitz.Rect) -> str | None:+ """Classify fully contained edge text; keep text crossing a band boundary."""+ band_height = page_rect.height * HEADER_FOOTER_BAND_RATIO+ if page_rect.y0 <= bbox.y0 and bbox.y1 <= page_rect.y0 + band_height:+ return "header"+ if page_rect.y1 - band_height <= bbox.y0 and bbox.y1 <= page_rect.y1:+ return "footer"+ return None+++def detect_headers_footers(+ doc: fitz.Document, threshold_ratio: float = 0.6,+) -> tuple[set[str], set[str]]:"""Detect headers and footers statistically.Principle: Headers and footers typically appear at fixed positions (top or bottom)on each page with the same content. We collect top and bottom text from all pages,and if certain text appears more frequently than the threshold, it is treated as noise.+ Return header and footer noise separately so filtering stays in the matching band."""if len(doc) < 3:- return set()+ return set(), set()headers = []footers = []@@ -323,12 +337,6 @@ def detect_headers_footers(doc: fitz.Document, threshold_ratio: float = 0.6) ->for i in pages_to_scan:page = doc[i]- rect = page.rect- h = rect.height-- # Define top and bottom regions (15% each)- top_rect = fitz.Rect(0, 0, rect.width, h * 0.15)- bottom_rect = fitz.Rect(0, h * 0.85, rect.width, h)# Extract text blocksblocks = page.get_text("blocks")@@ -338,24 +346,25 @@ def detect_headers_footers(doc: fitz.Document, threshold_ratio: float = 0.6) ->if not text:continue- # Simple spatial determination- if b_rect.intersects(top_rect):+ band = _header_footer_band(b_rect, page.rect)+ if band == "header":headers.append(text)- elif b_rect.intersects(bottom_rect):+ elif band == "footer":footers.append(text)# Count frequencies- noise_texts = set()+ header_noise = set()+ footer_noise = set()total_scanned = len(pages_to_scan)- for collection in [headers, footers]:+ for collection, noise_texts in [(headers, header_noise), (footers, footer_noise)]:counter = Counter(collection)for text, count in counter.items():# if text appears in > 60% of scanned pages, mark as noiseif count / total_scanned > threshold_ratio:noise_texts.add(text)- return noise_texts+ return header_noise, footer_noisedef _is_hangul(char: str) -> bool:@@ -416,6 +425,8 @@ def merge_adjacent_headings(elements: list) -> list:next_el = elements[j]if next_el.get("type") != 0 or not next_el.get("is_heading"):break+ if el.get("is_footer") or next_el.get("is_footer"):+ breaknext_match = re.match(r'^(#{1,6})\s+(.+)$', next_el["content"])if not next_match or next_match.group(1) != level:@@ -1570,10 +1581,12 @@ def extract_pdf_to_markdown(f"H1={size_map.get('h1', 'N/A')}, H2={size_map.get('h2', 'N/A')}, H3={size_map.get('h3', 'N/A')}")print(f"[INFO] Detecting repeated headers/footers...")- noise_texts = detect_headers_footers(doc)- if noise_texts:- print(f" Found {len(noise_texts)} repeated noise texts (will be removed):")- for t in list(noise_texts)[:3]:+ header_noise, footer_noise = detect_headers_footers(doc)+ noise_by_band = {"header": header_noise, "footer": footer_noise, None: set()}+ repeated_texts = header_noise | footer_noise+ if repeated_texts:+ print(f" Found {len(repeated_texts)} repeated noise texts (will be removed):")+ for t in list(repeated_texts)[:3]:print(f" - {t[:30]}...")markdown_content = f"# {title}\n\n"@@ -1646,7 +1659,8 @@ def extract_pdf_to_markdown(if block["type"] == 0:# Check if this is noise text to be filtered (whole block match)block_text_full = "".join([span["text"] for line in block["lines"] for span in line["spans"]]).strip()- if block_text_full in noise_texts:+ block_band = _header_footer_band(block_rect, page.rect)+ if block_text_full in noise_by_band[block_band]:continuefor line in block["lines"]:@@ -1694,9 +1708,15 @@ def extract_pdf_to_markdown(continue# Secondary check: line-level noise match (sometimes blocks are split)- if line_text in noise_texts:+ band = _header_footer_band(fitz.Rect(line["bbox"]), page.rect)+ if line_text in noise_by_band[band]:continue+ if band == "footer":+ line_text = remove_page_footer(line_text)+ if not line_text:+ continue+line_text = merge_adjacent_formatting(line_text)heading_level = get_heading_level(line_size, size_map, line_text, line_flags)@@ -1720,7 +1740,8 @@ def extract_pdf_to_markdown("content": final_text,"is_heading": heading_level > 0,"is_list": is_list,- "is_code": is_code_line+ "is_code": is_code_line,+ "is_footer": band == "footer",})elif block["type"] == 1:@@ -1752,13 +1773,15 @@ def extract_pdf_to_markdown(next_el = page_elements[j]if next_el["type"] != 0:break+ if el.get("is_footer") or next_el.get("is_footer"):+ breakif not should_merge_lines({"content": merged_content, "is_heading": False, "is_list": False}, next_el):breakmerged_content = join_wrapped_text(merged_content, next_el["content"])j += 1merged_elements.append({"type": 0,- "content": remove_page_footer(merged_content),+ "content": merged_content,"is_heading": False,"is_list": False})diff --git a/skills/ppt-master/scripts/tests/test_pdf_header_footer_position.py b/skills/ppt-master/scripts/tests/test_pdf_header_footer_position.pynew file mode 100644index 00000000..8f938496--- /dev/null+++ b/skills/ppt-master/scripts/tests/test_pdf_header_footer_position.py@@ -0,0 +1,79 @@+#!/usr/bin/env python3+"""Regression tests for position-aware PDF header and footer removal.++Repeated edge text must not erase signatures, and footer lines must not join+body dates before date/page-number cleanup.+"""++from __future__ import annotations++import sys+import tempfile+import unittest+from pathlib import Path++SCRIPTS_DIR = Path(__file__).resolve().parents[1]+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 pdf_to_md # noqa: E402+++class HeaderFooterPositionTests(unittest.TestCase):+ def _convert(self, last_page_lines: list[tuple[float, str]]) -> str:+ with tempfile.TemporaryDirectory() as tmp:+ source = Path(tmp) / "signature.pdf"+ with fitz.open() as doc:+ for number in range(1, 6):+ page = doc.new_page(width=600, height=800)+ page.insert_text((72, 40), "Example Company", fontname="helv", fontsize=12)+ page.insert_text((72, 200), f"Body of section {number}.", fontname="helv", fontsize=12)+ page.insert_text((72, 700), "Example Footer", fontname="helv", fontsize=12)+ if number == 5:+ for y, text in last_page_lines:+ page.insert_text((72, y), text, fontname="helv", fontsize=12)+ page.insert_text((290, 770), str(number), fontname="helv", fontsize=12)+ doc.save(source)+ return pdf_to_md.extract_pdf_to_markdown(str(source), images="none")++ def test_signature_equal_to_header_is_kept_and_header_removed(self) -> None:+ markdown = self._convert([(400, "Example Company"), (430, "October 2026")])+ self.assertEqual(markdown.count("Example Company"), 1)+ self.assertIn("October 2026", markdown)+ self.assertIn("Example Company", markdown.split("<!-- Page 5 -->")[1])++ def test_footer_page_number_does_not_swallow_body_date(self) -> None:+ markdown = self._convert([(430, "October 2026")])+ self.assertIn("October 2026", markdown)+ self.assertNotIn("October 2026 5", markdown)+ self.assertNotIn("Example Company", markdown)++ def test_signature_equal_to_header_inside_footer_is_kept(self) -> None:+ markdown = self._convert([(730, "Example Company"), (750, "October 2026")])+ self.assertEqual(markdown.count("Example Company"), 1)+ self.assertIn("October 2026", markdown)+ self.assertIn("Example Company", markdown.split("<!-- Page 5 -->")[1])+ self.assertNotIn("Example Footer", markdown)++ def test_date_and_page_number_inside_footer_are_removed(self) -> None:+ markdown = self._convert([(730, "November 2025 8")])+ self.assertNotIn("November 2025", markdown)+ self.assertIn("Body of section 5.", markdown)++ def test_date_and_page_number_inside_body_are_kept(self) -> None:+ markdown = self._convert([(430, "November 2025 8")])+ self.assertIn("November 2025 8", markdown)++ def test_line_crossing_header_boundary_is_kept(self) -> None:+ markdown = self._convert([(125, "Example Company")])+ self.assertEqual(markdown.count("Example Company"), 1)++ def test_repeated_footer_text_inside_body_is_kept(self) -> None:+ markdown = self._convert([(400, "Example Footer")])+ self.assertEqual(markdown.count("Example Footer"), 1)++ def test_body_line_in_block_crossing_header_boundary_is_kept(self) -> None:+ markdown = self._convert([(110, "Example Company"), (125, "Example Company")])+ self.assertEqual(markdown.count("Example Company"), 1)