通过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

相关推荐
weixin_4166600716 小时前
插件分享:将AI生成的数学公式无损导出为Word文档
人工智能·ai·word·论文·数学公式·deepseek
缺点内向20 小时前
C# 中如何从 URL 下载 Word 文档:基于 Spire.Doc 的高效解决方案
开发语言·c#·word
dyxal20 小时前
论文格式魔法全书:用Word通配符和宏一键完成专业排版
开发语言·word
wangbing11252 天前
将swagger在线文档转为word
microsoft·c#·word
kiki-bf2 天前
使用python把图片转为word
开发语言·python·word
开开心心_Every3 天前
Word转PDF工具,免费生成图片型文档
网络·笔记·pdf·word·powerpoint·excel·azure
dlhto3 天前
Markdown转Word操作指南
linux·centos·word
重生之光头强下海当程序猿4 天前
解决word中图像显示不全或者添加题注选项消失的问题
word
Array*4 天前
java实现word中插入附件(支持所有文件格式)
java·开发语言·word·poi·ole
艾上编程4 天前
第一章——办公自动化之Word报告自动生成:解放双手,高效创作
word