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

相关推荐
2501_9307077817 小时前
使用C#代码在 Word 文档中查找并替换文本
开发语言·c#·word
DreamNotOver2 天前
批量转换论文正文引用为上标后转PDF保持上标
pdf·word·引用·上标
黑咩狗夜.cm3 天前
Aspose.word实现表格每页固定表头、最后一行填满整个页面
开发语言·c#·word
gCode Teacher 格码致知3 天前
Python教学基础:用Python和openpyxl结合Word模板域写入数据-由Deepseek产生
python·word
蒲公英10013 天前
在wps软件的word中使用js宏命令设置表格背景色
javascript·word·wps
封奚泽优3 天前
word导入样式模版
word
随便叫个啥呢3 天前
java使用poi-tl模版+vform自定义表单生成word,使用LibreOffice导出为pdf
java·pdf·word
随便叫个啥呢4 天前
java使用poi-tl模版+vform自定义表单生成word,使用LibreOffice导出为pdf,批量下载为压缩文件
java·pdf·word·zip
CodeCraft Studio4 天前
国产化Word处理控件Spire.Doc教程:使用Java将RTF文件转换为PDF的全面教程
java·pdf·word·spire.doc·rtf转pdf·文件格式转换·文档开发sdk
随便叫个啥呢4 天前
java使用poi-tl模版+vform自定义表单生成word
java·word·poi-tl