add
This commit is contained in:
135
generate_word.py
Normal file
135
generate_word.py
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
将实习报告Markdown转换为Word文档
|
||||
"""
|
||||
|
||||
from docx import Document
|
||||
from docx.shared import Pt, RGBColor, Inches
|
||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||
from docx.oxml.ns import qn
|
||||
import re
|
||||
|
||||
def set_cell_border(cell):
|
||||
"""设置表格边框"""
|
||||
from docx.oxml import OxmlElement
|
||||
from docx.oxml.ns import qn
|
||||
|
||||
tcPr = cell._element.get_or_add_tcPr()
|
||||
tcBorders = OxmlElement('w:tcBorders')
|
||||
|
||||
for border_name in ['top', 'left', 'bottom', 'right']:
|
||||
border = OxmlElement(f'w:{border_name}')
|
||||
border.set(qn('w:val'), 'single')
|
||||
border.set(qn('w:sz'), '4')
|
||||
border.set(qn('w:space'), '0')
|
||||
border.set(qn('w:color'), '000000')
|
||||
tcBorders.append(border)
|
||||
|
||||
tcPr.append(tcBorders)
|
||||
|
||||
def add_paragraph_with_format(doc, text, bold=False, font_size=12, alignment=WD_ALIGN_PARAGRAPH.LEFT):
|
||||
"""添加带格式的段落"""
|
||||
para = doc.add_paragraph()
|
||||
para.alignment = alignment
|
||||
run = para.add_run(text)
|
||||
run.font.size = Pt(font_size)
|
||||
run.font.name = '宋体'
|
||||
run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
|
||||
if bold:
|
||||
run.bold = True
|
||||
return para
|
||||
|
||||
def add_heading(doc, text, level=1):
|
||||
"""添加标题"""
|
||||
heading = doc.add_heading(text, level=level)
|
||||
for run in heading.runs:
|
||||
run.font.size = Pt(16 - level * 2)
|
||||
run.font.name = '黑体'
|
||||
run._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
|
||||
return heading
|
||||
|
||||
def parse_markdown_to_docx(md_file, docx_file):
|
||||
"""解析Markdown文件并转换为Word文档"""
|
||||
|
||||
# 创建Word文档
|
||||
doc = Document()
|
||||
|
||||
# 设置页面边距
|
||||
sections = doc.sections
|
||||
for section in sections:
|
||||
section.top_margin = Inches(1)
|
||||
section.bottom_margin = Inches(1)
|
||||
section.left_margin = Inches(1.25)
|
||||
section.right_margin = Inches(1.25)
|
||||
|
||||
# 读取Markdown文件
|
||||
with open(md_file, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 分割成行
|
||||
lines = content.split('\n')
|
||||
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
line = lines[i].strip()
|
||||
|
||||
# 跳过空行
|
||||
if not line:
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理标题
|
||||
if line.startswith('#'):
|
||||
level = line.count('#')
|
||||
title_text = line.lstrip('#').strip()
|
||||
if level <= 6:
|
||||
add_heading(doc, title_text, min(level, 3))
|
||||
else:
|
||||
add_paragraph_with_format(doc, title_text, font_size=14, bold=True)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理水平线
|
||||
if line.startswith('---'):
|
||||
doc.add_paragraph('_' * 50)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理粗体文本
|
||||
if line.startswith('**') and line.endswith('**'):
|
||||
text = line.replace('**', '').strip()
|
||||
add_paragraph_with_format(doc, text, bold=True)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理列表
|
||||
if line.startswith('- ') or line.startswith('• '):
|
||||
text = line.lstrip('-·').strip()
|
||||
add_paragraph_with_format(doc, ' ' + text)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理编号列表
|
||||
if re.match(r'^\d+[、..]', line):
|
||||
add_paragraph_with_format(doc, line)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# 处理普通段落(可能是多行)
|
||||
para_text = line
|
||||
i += 1
|
||||
while i < len(lines) and lines[i].strip() and not lines[i].startswith('#') and not lines[i].startswith('-') and not lines[i].startswith('**'):
|
||||
para_text += ' ' + lines[i].strip()
|
||||
i += 1
|
||||
|
||||
add_paragraph_with_format(doc, para_text)
|
||||
|
||||
# 保存文档
|
||||
doc.save(docx_file)
|
||||
print(f"Word文档已生成: {docx_file}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
md_file = '杨璐-实习报告.md'
|
||||
docx_file = '杨璐-实习报告.docx'
|
||||
parse_markdown_to_docx(md_file, docx_file)
|
||||
Reference in New Issue
Block a user