import gradio as gr import re from typing import Dict, Any import os from pathlib import Path # Import dependencies for PDF and DOCX processing try: import docx DOCX_AVAILABLE = True except ImportError: DOCX_AVAILABLE = False try: import fitz # PyMuPDF PDF_AVAILABLE = True except ImportError: PDF_AVAILABLE = False class DocumentToMarkdownConverter: """Simple document to markdown converter""" def __init__(self): pass def extract_from_docx(self, docx_path: str) -> str: """Extract content from DOCX and convert to Markdown""" if not DOCX_AVAILABLE: raise ImportError("python-docx not installed") doc = docx.Document(docx_path) markdown_content = [] # Process paragraphs for paragraph in doc.paragraphs: if paragraph.text.strip(): md_text = self._convert_paragraph_to_markdown(paragraph) if md_text: markdown_content.append(md_text) # Process tables for table in doc.tables: md_table = self._convert_table_to_markdown(table) if md_table: markdown_content.append(md_table) return "\n\n".join(markdown_content) def extract_from_pdf(self, pdf_path: str) -> str: """Extract content from PDF and convert to Markdown""" if not PDF_AVAILABLE: raise ImportError("PyMuPDF not installed") doc = fitz.open(pdf_path) markdown_content = [] for page_num in range(len(doc)): page = doc.load_page(page_num) # Extract text blocks with formatting blocks = page.get_text("dict") page_markdown = self._convert_pdf_blocks_to_markdown(blocks) if page_markdown.strip(): page_header = f"## Page {page_num + 1}" markdown_content.append(page_header + "\n\n" + page_markdown) doc.close() return "\n\n---\n\n".join(markdown_content) def _convert_paragraph_to_markdown(self, paragraph) -> str: """Convert DOCX paragraph to Markdown""" text = paragraph.text.strip() if not text: return "" style_name = paragraph.style.name if paragraph.style else "Normal" # Check if paragraph has bold formatting is_bold = any(run.bold for run in paragraph.runs if run.bold) # Check font size for heading detection font_size = 12 if paragraph.runs: first_run = paragraph.runs[0] if first_run.font.size: font_size = first_run.font.size.pt # Convert based on style and formatting if "Title" in style_name or (is_bold and font_size >= 18): return f"# {text}" elif "Heading 1" in style_name or (is_bold and font_size >= 16): return f"# {text}" elif "Heading 2" in style_name or (is_bold and font_size >= 14): return f"## {text}" elif "Heading 3" in style_name or (is_bold and font_size >= 13): return f"### {text}" elif "Heading 4" in style_name: return f"#### {text}" elif "Heading 5" in style_name: return f"##### {text}" elif "Heading 6" in style_name: return f"###### {text}" elif re.match(r"^[\d\w]\.\s|^[â€ĸ\-\*]\s|^\d+\)\s", text): # List items if text.startswith(("1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.")): return f"1. {text[2:].strip()}" else: char_to_check = text[0] if text else "" if char_to_check in "â€ĸ-*": return f"- {text[1:].strip()}" else: return f"- {text}" else: # Regular paragraph formatted_text = self._apply_inline_formatting(paragraph) return formatted_text def _apply_inline_formatting(self, paragraph) -> str: """Apply inline formatting (bold, italic) to text""" result = "" for run in paragraph.runs: text = run.text if run.bold and run.italic: text = f"***{text}***" elif run.bold: text = f"**{text}**" elif run.italic: text = f"*{text}*" result += text return result def _convert_table_to_markdown(self, table) -> str: """Convert DOCX table to Markdown table""" if not table.rows: return "" markdown_rows = [] # Process header row header_cells = [cell.text.strip() for cell in table.rows[0].cells] markdown_rows.append("| " + " | ".join(header_cells) + " |") markdown_rows.append("| " + " | ".join(["---"] * len(header_cells)) + " |") # Process data rows for row in table.rows[1:]: cells = [cell.text.strip() for cell in row.cells] markdown_rows.append("| " + " | ".join(cells) + " |") return "\n".join(markdown_rows) def _convert_pdf_blocks_to_markdown(self, blocks_dict) -> str: """Convert PDF text blocks to Markdown""" markdown_lines = [] for block in blocks_dict.get("blocks", []): if block.get("type") == 0: # Text block for line in block.get("lines", []): line_text = "" for span in line.get("spans", []): text = span.get("text", "").strip() if text: # Check formatting font_size = span.get("size", 12) flags = span.get("flags", 0) # Bold = flags & 16, Italic = flags & 2 is_bold = bool(flags & 16) is_italic = bool(flags & 2) # Apply formatting if is_bold and is_italic: text = f"***{text}***" elif is_bold: text = f"**{text}**" elif is_italic: text = f"*{text}*" # Check if it's a heading based on font size if font_size >= 18: text = f"# {text}" elif font_size >= 16: text = f"## {text}" elif font_size >= 14: text = f"### {text}" line_text += text + " " if line_text.strip(): markdown_lines.append(line_text.strip()) return "\n\n".join(markdown_lines) def analyze_markdown_structure(self, markdown_text: str) -> Dict[str, Any]: """Analyze the structure of extracted Markdown""" lines = markdown_text.split("\n") structure = { "headings": {"h1": 0, "h2": 0, "h3": 0, "h4": 0, "h5": 0, "h6": 0}, "lists": {"ordered": 0, "unordered": 0}, "tables": 0, "paragraphs": 0, "bold_text": 0, "italic_text": 0, "total_lines": len(lines), "word_count": len(markdown_text.split()), "character_count": len(markdown_text), } in_table = False for line in lines: line = line.strip() if not line: continue # Count headings if line.startswith("#"): level = len(line) - len(line.lstrip("#")) if level <= 6: structure["headings"][f"h{level}"] += 1 # Count lists elif re.match(r"^\d+\.\s", line): structure["lists"]["ordered"] += 1 elif re.match(r"^[\-\*\+]\s", line): structure["lists"]["unordered"] += 1 # Count tables elif "|" in line and not in_table: structure["tables"] += 1 in_table = True elif "|" not in line: in_table = False if ( line and not line.startswith("#") and not re.match(r"^[\-\*\+\d]", line) ): structure["paragraphs"] += 1 # Count formatting structure["bold_text"] += len(re.findall(r"\*\*[^*]+\*\*", line)) structure["italic_text"] += len(re.findall(r"\*[^*]+\*", line)) return structure def extract_document_to_markdown(file_path: str) -> Dict[str, Any]: """ Extract document content and convert to Markdown format Args: file_path: Path to PDF or DOCX file Returns: Dictionary containing markdown content and structure analysis """ if not file_path or not os.path.exists(file_path): return {"error": "File not found", "markdown": "", "structure": {}} converter = DocumentToMarkdownConverter() file_extension = Path(file_path).suffix.lower() try: if file_extension == ".docx": if not DOCX_AVAILABLE: return { "error": "python-docx not installed. Run: pip install python-docx", "markdown": "", "structure": {}, } markdown_content = converter.extract_from_docx(file_path) elif file_extension == ".pdf": if not PDF_AVAILABLE: return { "error": "PyMuPDF not installed. Run: pip install PyMuPDF", "markdown": "", "structure": {}, } markdown_content = converter.extract_from_pdf(file_path) else: return { "error": f"Unsupported file type: {file_extension}. Only PDF and DOCX files are supported.", "markdown": "", "structure": {}, } # Analyze markdown structure structure = converter.analyze_markdown_structure(markdown_content) return { "success": True, "file_info": { "name": Path(file_path).name, "type": file_extension.upper()[1:], "size_kb": round(os.path.getsize(file_path) / 1024, 2), }, "markdown": markdown_content, "structure": structure, "preview": markdown_content[:500] + "..." if len(markdown_content) > 500 else markdown_content, } except Exception as e: return { "error": f"Error processing file: {str(e)}", "markdown": "", "structure": {}, } def create_interface(): """Create the main Gradio interface""" with gr.Blocks( title="Document to Markdown Converter", theme=gr.themes.Soft() ) as demo: gr.Markdown(""" # 📄 Document to Markdown Converter Convert PDF and DOCX files to Markdown format with structure analysis. **Supported formats:** PDF (.pdf), Word Documents (.docx) """) # Show dependency status missing_deps = [] if not DOCX_AVAILABLE: missing_deps.append("python-docx") if not PDF_AVAILABLE: missing_deps.append("PyMuPDF") if missing_deps: gr.Markdown( f"âš ī¸ **Missing dependencies**: Some features may be limited. Missing: {', '.join(missing_deps)}" ) else: gr.Markdown("✅ **All dependencies available**: Full functionality enabled") with gr.Row(): with gr.Column(scale=1): # File upload file_input = gr.File( label="📎 Upload Document", file_types=[".pdf", ".docx"], type="filepath", ) # Process button extract_btn = gr.Button( "🔄 Convert to Markdown", variant="primary", size="lg" ) # Options with gr.Accordion("âš™ī¸ Options", open=False): show_structure = gr.Checkbox( label="📊 Show Structure Analysis", value=True ) show_preview = gr.Checkbox( label="đŸ‘ī¸ Show Preview Only (first 500 chars)", value=False ) with gr.Column(scale=2): # Output tabs with gr.Tabs(): with gr.TabItem("📝 Markdown Output"): markdown_output = gr.Textbox( label="Generated Markdown", lines=20, max_lines=40, show_copy_button=True, placeholder="Converted markdown will appear here...", ) with gr.TabItem("📊 Structure Analysis"): structure_output = gr.JSON(label="Document Structure") with gr.TabItem("â„šī¸ File Information"): info_output = gr.JSON(label="File Details") # Event handler def process_document(file_path, show_struct, show_prev): """Process uploaded document""" if not file_path: return "No file uploaded", {}, {} result = extract_document_to_markdown(file_path) if "error" in result: return f"❌ Error: {result['error']}", {}, {} # Determine what to show markdown_text = result["preview"] if show_prev else result["markdown"] structure = result["structure"] if show_struct else {} file_info = result["file_info"] return markdown_text, structure, file_info # Connect the button extract_btn.click( fn=process_document, inputs=[file_input, show_structure, show_preview], outputs=[markdown_output, structure_output, info_output], ) # Examples section gr.Markdown(""" ## 📖 Usage Examples 1. **Upload a PDF or DOCX file** using the file uploader above 2. **Click "Convert to Markdown"** to process the document 3. **View results** in the tabs: - **Markdown Output**: The converted markdown text - **Structure Analysis**: Document statistics and structure - **File Information**: Basic file details ### ✨ Features - **Smart heading detection** based on font size and styles - **Table extraction** and markdown formatting - **List detection** and proper markdown conversion - **Inline formatting** preservation (bold, italic) - **Structure analysis** with statistics """) return demo if __name__ == "__main__": # Create and launch the interface demo = create_interface() demo.launch(server_name="0.0.0.0", mcp_server=True, server_port=7860, share=True)