通过markdown表格批量生成格式化的word教学单元设计表格

素材:

模板:

代码:

python 复制代码
import pandas as pd
from python_docx_replace import docx_replace,docx_get_keys
from docx import Document
from docxcompose.composer import Composer

def parse_markdown_tables(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
    current_table = []
    for line in lines:
        current_table.append(line.strip().replace('<br>', '\n').split('|')[1:-1]) 
    df = pd.DataFrame(current_table[1:], columns=current_table[0])

    return df


df=parse_markdown_tables("单元设计.md")

def convert(num,df):
    print(num)
    doc = Document("template.docx")
    # keys = docx_get_keys(doc) # Let's suppose the Word document has the keys: ${name} and ${phone}
    # print(keys)  #['name', 'week', 'problem', 'location', 'test', 'obj', 'book', 'date', 'seq', 'homework', 'review', 'goal']

    docx_replace(doc,
                book=df.iloc[num].tolist()[8].strip(),
                week=df.iloc[num].tolist()[0].strip(),
                problem=df.iloc[num].tolist()[6].strip(),
                review=df.iloc[num].tolist()[11].strip(),
                name=df.iloc[num].tolist()[3].strip(),
                location=df.iloc[num].tolist()[7].strip(),
                date=df.iloc[num].tolist()[1].strip(),
                homework=df.iloc[num].tolist()[10].strip(),
                seq=df.iloc[num].tolist()[2].strip(),
                test=df.iloc[num].tolist()[9].strip(),
                obj=df.iloc[num].tolist()[4].strip(),
                goal=df.iloc[num].tolist()[5].strip())
    return doc

master = Document("master.docx")# 任意word文档,里面可以包含封面
composer = Composer(master)

for i in range(len(df.axes[0])):
    unit_doc=convert(i,df)
    composer.append(unit_doc)
composer.save("combined.docx")

效果,16张表:

项目地址:https://github.com/fjh1997/Unit

相关推荐
qq_5469372712 天前
Excel批量转PDF_Word_图片,支持自动合并报表,效率翻倍。
pdf·word·excel
(Charon)13 天前
【C++ 面试高频:内存管理、RAII 和智能指针详解】
java·开发语言·word
江畔柳前堤13 天前
github实战指南03-Pull Request 全流程实战
开发语言·人工智能·python·深度学习·github·word
2603_9541383914 天前
PDF 转 Word 工具深度评测:从参数解析到实战避坑
pdf·word
知南x14 天前
【DPDK例程学习】(4) l2fwd
学习·word
江畔柳前堤14 天前
github实战指南00-命令在哪里执行?
人工智能·线性代数·oracle·数据挖掘·github·word
江畔柳前堤14 天前
github实战指南05-Fork与开源协作
人工智能·线性代数·oracle·开源·github·word
yivifu15 天前
怎样将Word文档中脚注引用后面的空格轻松删除
word·vba
Sour15 天前
Word 文档翻译后保留格式的检查清单:标题、表格、图片、目录和批注
pdf·word·办公软件·office·文档翻译
qq_4221525716 天前
Word 文件太大怎么压缩?2026 年文档瘦身方案对比
开发语言·c#·word