고급 수식 템플릿 워크북

고급 수식을 기존 문서와 표 셀에 넣고 마커 교체·중첩 표·머리글·바닥글·가로 페이지를 함께 사용합니다.

입력과 출력 비교

실제 HTML/MathML 입력을 브라우저에서 보고, 생성된 DOCX를 LibreOffice로 내보낸 PDF와 비교하세요. 수식 편집은 DOCX를 Word에서 열어 확인할 수 있습니다. 이 예제는 Word 객체로 직접 만든 내용도 포함합니다. 브라우저에는 삽입한 HTML/MathML 조각만 호출 순서대로 표시하며, PDF에는 주변 Word 문서와 해당 스타일도 포함합니다.

브라우저 · HTML / MathML

HTML 미리보기 열기

LibreOffice · PDF

PDF 열기

Markdown·LaTeX 예제는 변환 과정에서 만든 HTML·MathML을 표시합니다. 브라우저의 MathML 지원, Word 스타일, 글꼴, 페이지 배치에 따라 표시가 달라질 수 있습니다.

PDF 패널은 페이지 이미지를 표시합니다. 확대·검색·텍스트 선택은 원본 PDF 열기를, 편집은 DOCX 다운로드를 이용하세요.

실행

예제 ZIP의 압축을 풀고 예제 의존성을 설치한 뒤, examples/ 폴더가 들어 있는 위치에서 실행하세요.

python -m examples.26_template_workbook

생성된 DOCX

참조 PDF

전체 코드

"""Advanced equations in existing documents, markers, cells, and nested tables."""

from pathlib import Path

from docx import Document
from docx.oxml.ns import qn
from docx.shared import Inches

from mathml2docx import MathmlToDocx, TableStyle

from .advanced_math import CATALOG, source
from .support import save

# Use a wider page for block matrices instead of squeezing them into a narrow cell.
document = Document()
section = document.sections[0]
section.page_width = Inches(11.7)
section.page_height = Inches(8.3)
for side in ("left_margin", "right_margin"):
    setattr(section, side, Inches(0.7))
style = document.styles["Normal"]
style.font.name = "Malgun Gothic"
style.element.get_or_add_rPr().get_or_add_rFonts().set(
    qn("w:eastAsia"), "Malgun Gothic"
)
section.header.paragraphs[0].text = "Numerical methods workbook / 수식 검토 기록"
section.footer.paragraphs[0].text = "Inspect, edit, save, and reopen in Word."
document.add_heading("Template workbook / 검토 워크북", 0)
marker = document.add_paragraph("Prepared for {{rea")
marker.add_run("der}}")
parser = MathmlToDocx(
    table_style=TableStyle.GRID,
    stylesheet="* {font-family:'Malgun Gothic'} p,td {font-size:10pt} .review {color:#244488}",
    img_src_base_path=str(Path(__file__).resolve().parent / "assets"),
)
parser.replace_html(document, "{{reader}}", "<b>연구 검토자 / Reviewer</b>")
for entry in (entry for entry in CATALOG if entry["format"] == "mathml"):
    parser.add_html_to_document(
        "<h2>" + entry["id"] + ". " + entry["title"] + "</h2>", document
    )
    cell = document.add_table(rows=1, cols=1).cell(0, 0)
    parser.add_html_to_cell(
        '<p class="review"><b>검토 대상 / Expression</b></p><p>'
        + source(entry)
        + "</p>"
        "<table><tr><th>Check</th><th>Result</th></tr><tr><td>역할·차원</td>"
        "<td><em>Verify every matrix dimension and grouped script.</em></td></tr></table>"
        '<p><a href="https://example.com/workbook">Workbook reference</a> '
        '<img src="swatch.png" width="24"></p>',
        cell,
    )
    parser.add_html_to_document(
        "<ul><li>수식을 클릭하여 편집합니다.</li><li>Save, reopen, and inspect the table.</li></ul>",
        document,
    )
save(document, "26_template_workbook.docx")

수식 입력 파일

독립 정답 (JSON)