当前位置: 首页 > article >正文

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

素材:在这里插入图片描述
模板:
在这里插入图片描述
代码:

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


http://www.kler.cn/news/313078.html

相关文章:

  • 基于深度学习的花卉智能分类识别系统
  • 【win工具】win安装flameshot并设置截图快捷键
  • 【Python报错已解决】xlrd.biffh.XLRDError: Excel xlsx file; not supported
  • C++自动寻径算法
  • 《黑神话悟空》开发框架与战斗系统解析
  • 4.C++中程序中的命名空间
  • git 本地分支误删,怎么恢复?误删本地已提交未推送的分支!
  • 基于Python实现一个浪漫烟花秀
  • 如何设置 Django 错误邮件通知 ?
  • I2C中继器TCA9517A(TI)
  • 新160个crackme - 060-snake
  • 2024年Q3国际信息系统安全认证联盟(ISC2)内部研讨会要点分享
  • 系统架构设计师 大数据架构篇二
  • 球形包围框-Bounding Sphere-原理-代码实现
  • Mycat中间件
  • 牛客BC92,逆序输出
  • 222222222
  • Qt/C++开发经验
  • 【深度学习 transformer】理解 Transformer:机器学习界的“变形金刚
  • Vue3:v-model实现组件通信
  • Streamlit:使用 Python 快速开发 Web 应用
  • 大数据新视界 --大数据大厂之AI 与大数据的融合:开创智能未来的新篇章
  • Git入门学习(1)
  • HTTP中的Cookie与Session
  • pandoc自定义过滤器
  • 小程序构建npm失败
  • WPF 所有的控件和每个控件的主要作用和应用场景
  • 25届计算机专业毕设选题推荐-基于python+Django协调过滤的新闻推荐系统
  • 数学辅导微信小程序--论文ppt源码调试讲解
  • 执行网络攻击模拟的 7 个步骤