md-to-office

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Markdown to Office Skill

Markdown转Office技能

Overview

概述

This skill enables conversion from Markdown to various Office formats using Pandoc - the universal document converter. Convert your Markdown files to professional Word documents, PowerPoint presentations, PDFs, and more while preserving formatting and structure.
本技能借助Pandoc——通用文档转换器,实现Markdown到多种Office格式的转换。将你的Markdown文件转换为专业的Word文档、PowerPoint演示文稿、PDF等格式,同时保留原有的格式和结构。

How to Use

使用方法

  1. Provide the Markdown content or file
  2. Specify the target format (docx, pptx, pdf, etc.)
  3. Optionally provide a reference template for styling
  4. I'll convert using Pandoc with optimal settings
Example prompts:
  • "Convert this README.md to a professional Word document"
  • "Turn my markdown notes into a PowerPoint presentation"
  • "Generate a PDF from this markdown with custom styling"
  • "Create a Word doc from this markdown using company template"
  1. 提供Markdown内容或文件
  2. 指定目标格式(docx, pptx, pdf等)
  3. (可选)提供用于样式设置的参考模板
  4. 我会使用Pandoc并通过优化配置完成转换
示例提示:
  • "将此README.md转换为专业的Word文档"
  • "把我的Markdown笔记转成PowerPoint演示文稿"
  • "根据此Markdown内容生成带自定义样式的PDF"
  • "使用公司模板将此Markdown内容创建为Word文档"

Domain Knowledge

领域知识

Pandoc Fundamentals

Pandoc基础

bash
undefined
bash
undefined

Basic conversion

Basic conversion

pandoc input.md -o output.docx pandoc input.md -o output.pdf pandoc input.md -o output.pptx
pandoc input.md -o output.docx pandoc input.md -o output.pdf pandoc input.md -o output.pptx

With template

With template

pandoc input.md --reference-doc=template.docx -o output.docx
pandoc input.md --reference-doc=template.docx -o output.docx

Multiple inputs

Multiple inputs

pandoc ch1.md ch2.md ch3.md -o book.docx
undefined
pandoc ch1.md ch2.md ch3.md -o book.docx
undefined

Supported Conversions

支持的转换类型

FromToCommand
MarkdownWord
pandoc in.md -o out.docx
MarkdownPDF
pandoc in.md -o out.pdf
MarkdownPowerPoint
pandoc in.md -o out.pptx
MarkdownHTML
pandoc in.md -o out.html
MarkdownLaTeX
pandoc in.md -o out.tex
MarkdownEPUB
pandoc in.md -o out.epub
源格式目标格式命令
MarkdownWord
pandoc in.md -o out.docx
MarkdownPDF
pandoc in.md -o out.pdf
MarkdownPowerPoint
pandoc in.md -o out.pptx
MarkdownHTML
pandoc in.md -o out.html
MarkdownLaTeX
pandoc in.md -o out.tex
MarkdownEPUB
pandoc in.md -o out.epub

Markdown to Word (.docx)

Markdown转Word (.docx)

Basic Conversion

基础转换

bash
pandoc document.md -o document.docx
bash
pandoc document.md -o document.docx

With Template (Reference Doc)

使用模板(参考文档)

bash
undefined
bash
undefined

First create a template by converting sample

First create a template by converting sample

pandoc sample.md -o reference.docx
pandoc sample.md -o reference.docx

Edit reference.docx styles in Word, then use it

Edit reference.docx styles in Word, then use it

pandoc input.md --reference-doc=reference.docx -o output.docx
undefined
pandoc input.md --reference-doc=reference.docx -o output.docx
undefined

With Table of Contents

包含目录

bash
pandoc document.md --toc --toc-depth=3 -o document.docx
bash
pandoc document.md --toc --toc-depth=3 -o document.docx

With Metadata

包含元数据

bash
pandoc document.md \
  --metadata title="My Report" \
  --metadata author="John Doe" \
  --metadata date="2024-01-15" \
  -o document.docx
bash
pandoc document.md \
  --metadata title="My Report" \
  --metadata author="John Doe" \
  --metadata date="2024-01-15" \
  -o document.docx

Markdown to PDF

Markdown转PDF

Via LaTeX (Best Quality)

通过LaTeX(最佳质量)

bash
undefined
bash
undefined

Requires LaTeX installation

Requires LaTeX installation

pandoc document.md -o document.pdf
pandoc document.md -o document.pdf

With custom settings

With custom settings

pandoc document.md
--pdf-engine=xelatex
-V geometry:margin=1in
-V fontsize=12pt
-o document.pdf
undefined
pandoc document.md
--pdf-engine=xelatex
-V geometry:margin=1in
-V fontsize=12pt
-o document.pdf
undefined

Via HTML/wkhtmltopdf

通过HTML/wkhtmltopdf

bash
pandoc document.md \
  --pdf-engine=wkhtmltopdf \
  --css=style.css \
  -o document.pdf
bash
pandoc document.md \
  --pdf-engine=wkhtmltopdf \
  --css=style.css \
  -o document.pdf

PDF Options

PDF选项

bash
pandoc document.md \
  -V papersize:a4 \
  -V geometry:margin=2cm \
  -V fontfamily:libertinus \
  -V colorlinks:true \
  --toc \
  -o document.pdf
bash
pandoc document.md \
  -V papersize:a4 \
  -V geometry:margin=2cm \
  -V fontfamily:libertinus \
  -V colorlinks:true \
  --toc \
  -o document.pdf

Markdown to PowerPoint (.pptx)

Markdown转PowerPoint (.pptx)

Basic Conversion

基础转换

bash
pandoc slides.md -o presentation.pptx
bash
pandoc slides.md -o presentation.pptx

Markdown Structure for Slides

幻灯片的Markdown结构

markdown
---
title: Presentation Title
author: Author Name
date: January 2024
---
markdown
---
title: Presentation Title
author: Author Name
date: January 2024
---

Section Header (creates section divider)

Section Header (creates section divider)

Slide Title

Slide Title

  • Bullet point 1
  • Bullet point 2
    • Sub-bullet
  • Bullet point 1
  • Bullet point 2
    • Sub-bullet

Another Slide

Another Slide

Content here
::: notes Speaker notes go here (not visible in slides) :::
Content here
::: notes Speaker notes go here (not visible in slides) :::

Slide with Image

Slide with Image

Description{width=80%}
Description{width=80%}

Two Column Slide

Two Column Slide

:::::::::::::: {.columns} ::: {.column width="50%"} Left column content :::
::: {.column width="50%"} Right column content ::: ::::::::::::::
undefined
:::::::::::::: {.columns} ::: {.column width="50%"} Left column content :::
::: {.column width="50%"} Right column content ::: ::::::::::::::
undefined

With Template

使用模板

bash
undefined
bash
undefined

Use corporate PowerPoint template

Use corporate PowerPoint template

pandoc slides.md --reference-doc=template.pptx -o presentation.pptx
undefined
pandoc slides.md --reference-doc=template.pptx -o presentation.pptx
undefined

YAML Frontmatter

YAML前置元数据

Add metadata at the top of your Markdown:
yaml
---
title: "Document Title"
author: "Author Name"
date: "2024-01-15"
abstract: "Brief description"
toc: true
toc-depth: 2
numbersections: true
geometry: margin=1in
fontsize: 11pt
documentclass: report
---
在Markdown顶部添加元数据:
yaml
---
title: "Document Title"
author: "Author Name"
date: "2024-01-15"
abstract: "Brief description"
toc: true
toc-depth: 2
numbersections: true
geometry: margin=1in
fontsize: 11pt
documentclass: report
---

First Chapter

First Chapter

...
undefined
...
undefined

Python Integration

Python集成

python
import subprocess
import os

def md_to_docx(input_path, output_path, template=None):
    """Convert Markdown to Word document."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    return output_path

def md_to_pdf(input_path, output_path, **options):
    """Convert Markdown to PDF with options."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if options.get('toc'):
        cmd.append('--toc')
    
    if options.get('margin'):
        cmd.extend(['-V', f"geometry:margin={options['margin']}"])
    
    subprocess.run(cmd, check=True)
    return output_path

def md_to_pptx(input_path, output_path, template=None):
    """Convert Markdown to PowerPoint."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    return output_path
python
import subprocess
import os

def md_to_docx(input_path, output_path, template=None):
    """Convert Markdown to Word document."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    return output_path

def md_to_pdf(input_path, output_path, **options):
    """Convert Markdown to PDF with options."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if options.get('toc'):
        cmd.append('--toc')
    
    if options.get('margin'):
        cmd.extend(['-V', f"geometry:margin={options['margin']}"])
    
    subprocess.run(cmd, check=True)
    return output_path

def md_to_pptx(input_path, output_path, template=None):
    """Convert Markdown to PowerPoint."""
    cmd = ['pandoc', input_path, '-o', output_path]
    
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    return output_path

pypandoc (Python Wrapper)

pypandoc(Python包装器)

python
import pypandoc
python
import pypandoc

Simple conversion

Simple conversion

output = pypandoc.convert_file('input.md', 'docx', outputfile='output.docx')
output = pypandoc.convert_file('input.md', 'docx', outputfile='output.docx')

With options

With options

output = pypandoc.convert_file( 'input.md', 'docx', outputfile='output.docx', extra_args=['--toc', '--reference-doc=template.docx'] )
output = pypandoc.convert_file( 'input.md', 'docx', outputfile='output.docx', extra_args=['--toc', '--reference-doc=template.docx'] )

From string

From string

md_content = "# Hello\n\nThis is markdown." output = pypandoc.convert_text(md_content, 'docx', format='md', outputfile='output.docx')
undefined
md_content = "# Hello\n\nThis is markdown." output = pypandoc.convert_text(md_content, 'docx', format='md', outputfile='output.docx')
undefined

Best Practices

最佳实践

  1. Use Templates: Create a reference document for consistent branding
  2. Structure Headers: Use consistent heading levels (## for slides, # for sections)
  3. Test Incrementally: Convert small sections first to verify formatting
  4. Include Metadata: Use YAML frontmatter for document properties
  5. Handle Images: Use relative paths and specify dimensions
  1. 使用模板:创建参考文档以保持品牌一致性
  2. 结构化标题:使用统一的标题层级(幻灯片用##,章节用#)
  3. 增量测试:先转换小部分内容以验证格式
  4. 包含元数据:使用YAML前置元数据设置文档属性
  5. 处理图片:使用相对路径并指定尺寸

Common Patterns

常见模式

Batch Conversion

批量转换

python
import subprocess
from pathlib import Path

def batch_convert(input_dir, output_format, output_dir=None):
    """Convert all markdown files in a directory."""
    input_path = Path(input_dir)
    output_path = Path(output_dir) if output_dir else input_path
    
    for md_file in input_path.glob('*.md'):
        output_file = output_path / md_file.with_suffix(f'.{output_format}').name
        subprocess.run([
            'pandoc', str(md_file), '-o', str(output_file)
        ], check=True)
        print(f"Converted: {md_file.name} -> {output_file.name}")

batch_convert('./docs', 'docx', './output')
python
import subprocess
from pathlib import Path

def batch_convert(input_dir, output_format, output_dir=None):
    """Convert all markdown files in a directory."""
    input_path = Path(input_dir)
    output_path = Path(output_dir) if output_dir else input_path
    
    for md_file in input_path.glob('*.md'):
        output_file = output_path / md_file.with_suffix(f'.{output_format}').name
        subprocess.run([
            'pandoc', str(md_file), '-o', str(output_file)
        ], check=True)
        print(f"Converted: {md_file.name} -> {output_file.name}")

batch_convert('./docs', 'docx', './output')

Report Generator

报告生成器

python
def generate_report(title, sections, output_path, template=None):
    """Generate Word report from structured data."""
    
    # Build markdown
    md_content = f"""---
title: "{title}"
date: "{datetime.now().strftime('%B %d, %Y')}"
---

"""
    for section_title, content in sections.items():
        md_content += f"# {section_title}\n\n{content}\n\n"
    
    # Write temp file
    with open('temp_report.md', 'w') as f:
        f.write(md_content)
    
    # Convert
    cmd = ['pandoc', 'temp_report.md', '-o', output_path, '--toc']
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    os.remove('temp_report.md')
python
def generate_report(title, sections, output_path, template=None):
    """Generate Word report from structured data."""
    
    # Build markdown
    md_content = f"""---
title: "{title}"
date: "{datetime.now().strftime('%B %d, %Y')}"
---

"""
    for section_title, content in sections.items():
        md_content += f"# {section_title}\n\n{content}\n\n"
    
    # Write temp file
    with open('temp_report.md', 'w') as f:
        f.write(md_content)
    
    # Convert
    cmd = ['pandoc', 'temp_report.md', '-o', output_path, '--toc']
    if template:
        cmd.extend(['--reference-doc', template])
    
    subprocess.run(cmd, check=True)
    os.remove('temp_report.md')

Examples

示例

Example 1: Technical Documentation

示例1:技术文档转换

python
import subprocess
python
import subprocess

Create comprehensive markdown

Create comprehensive markdown

doc = """--- title: "API Documentation" author: "Dev Team" date: "2024-01-15" toc: true toc-depth: 2

doc = """--- title: "API Documentation" author: "Dev Team" date: "2024-01-15" toc: true toc-depth: 2

Introduction

Introduction

This document describes the REST API for our service.
This document describes the REST API for our service.

Authentication

Authentication

All API requests require an API key in the header:
Authorization: Bearer YOUR_API_KEY
All API requests require an API key in the header:
Authorization: Bearer YOUR_API_KEY

Endpoints

Endpoints

GET /users

GET /users

Retrieve all users.
Response:
FieldTypeDescription
idintegerUser ID
namestringFull name
emailstringEmail address
Retrieve all users.
Response:
FieldTypeDescription
idintegerUser ID
namestringFull name
emailstringEmail address

POST /users

POST /users

Create a new user.
Request Body:
json
{
  "name": "John Doe",
  "email": "john@example.com"
}
Create a new user.
Request Body:
json
{
  "name": "John Doe",
  "email": "john@example.com"
}

Error Codes

Error Codes

CodeMeaning
400Bad Request
401Unauthorized
404Not Found
500Server Error
"""
CodeMeaning
400Bad Request
401Unauthorized
404Not Found
500Server Error
"""

Save markdown

Save markdown

with open('api_docs.md', 'w') as f: f.write(doc)
with open('api_docs.md', 'w') as f: f.write(doc)

Convert to Word

Convert to Word

subprocess.run([ 'pandoc', 'api_docs.md', '-o', 'api_documentation.docx', '--toc', '--reference-doc', 'company_template.docx' ], check=True)
subprocess.run([ 'pandoc', 'api_docs.md', '-o', 'api_documentation.docx', '--toc', '--reference-doc', 'company_template.docx' ], check=True)

Convert to PDF

Convert to PDF

subprocess.run([ 'pandoc', 'api_docs.md', '-o', 'api_documentation.pdf', '--toc', '-V', 'geometry:margin=1in', '-V', 'fontsize=11pt' ], check=True)
undefined
subprocess.run([ 'pandoc', 'api_docs.md', '-o', 'api_documentation.pdf', '--toc', '-V', 'geometry:margin=1in', '-V', 'fontsize=11pt' ], check=True)
undefined

Example 2: Presentation from Markdown

示例2:从Markdown生成演示文稿

python
slides_md = """---
title: "Q4 Business Review"
author: "Sales Team"
date: "January 2024"
---
python
slides_md = """---
title: "Q4 Business Review"
author: "Sales Team"
date: "January 2024"
---

Overview

Overview

Agenda

Agenda

  • Q4 Performance Summary
  • Regional Highlights
  • 2024 Outlook
  • Q&A
  • Q4 Performance Summary
  • Regional Highlights
  • 2024 Outlook
  • Q&A

Q4 Performance

Q4 Performance

Key Metrics

Key Metrics

  • Revenue: $12.5M (+15% YoY)
  • New Customers: 250
  • Retention Rate: 94%
  • Revenue: $12.5M (+15% YoY)
  • New Customers: 250
  • Retention Rate: 94%

Regional Performance

Regional Performance

:::::::::::::: {.columns} ::: {.column width="50%"} North America
  • Revenue: $6.2M
  • Growth: +18% :::
::: {.column width="50%"} Europe
  • Revenue: $4.1M
  • Growth: +12% ::: ::::::::::::::
:::::::::::::: {.columns} ::: {.column width="50%"} North America
  • Revenue: $6.2M
  • Growth: +18% :::
::: {.column width="50%"} Europe
  • Revenue: $4.1M
  • Growth: +12% ::: ::::::::::::::

2024 Outlook

2024 Outlook

Strategic Priorities

Strategic Priorities

  1. Expand APAC presence
  2. Launch new product line
  3. Improve customer onboarding
  1. Expand APAC presence
  2. Launch new product line
  3. Improve customer onboarding

Revenue Targets

Revenue Targets

QuarterTarget
Q1$13M
Q2$14M
Q3$15M
Q4$16M
QuarterTarget
Q1$13M
Q2$14M
Q3$15M
Q4$16M

Thank You

Thank You

Questions?

Questions?

Contact: sales@company.com """
with open('presentation.md', 'w') as f: f.write(slides_md)
subprocess.run([ 'pandoc', 'presentation.md', '-o', 'q4_review.pptx', '--reference-doc', 'company_slides.pptx' ], check=True)
undefined
Contact: sales@company.com """
with open('presentation.md', 'w') as f: f.write(slides_md)
subprocess.run([ 'pandoc', 'presentation.md', '-o', 'q4_review.pptx', '--reference-doc', 'company_slides.pptx' ], check=True)
undefined

Limitations

局限性

  • Complex Word formatting may not convert perfectly
  • PDF conversion requires LaTeX or wkhtmltopdf
  • PowerPoint animations not supported
  • Some advanced tables may need manual adjustment
  • Image positioning can be tricky
  • 复杂的Word格式可能无法完美转换
  • PDF转换需要安装LaTeX或wkhtmltopdf
  • 不支持PowerPoint动画效果
  • 部分复杂表格可能需要手动调整
  • 图片定位可能存在难度

Installation

安装方法

bash
undefined
bash
undefined

macOS

macOS

brew install pandoc
brew install pandoc

Ubuntu/Debian

Ubuntu/Debian

sudo apt-get install pandoc
sudo apt-get install pandoc

Windows

Windows

choco install pandoc
choco install pandoc

Python wrapper

Python wrapper

pip install pypandoc
undefined
pip install pypandoc
undefined

Resources

资源