栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python生成三年级数学竖式计算并用Python-pptx写入PPT

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python生成三年级数学竖式计算并用Python-pptx写入PPT

       三年级下册的学生需要经常练习两位数乘两位数,三位数除以一位数的竖式计算,虽然出题难题简单,但是如果可以用程序一下生成,岂不是一劳永逸。那就动手吧。

       首先需要安装Python-pptx,安装过程可能会因为Pip版本过低而安装失败,根据错误提示重新安装Pip版本即可解决问题。

       紧接着,写一个生成一位数、两位数、三位数的函数:

def randomNumber(countNumer):
    number=0

    #一位数
    if (countNumer == 1):
        while number < 2 or number>9:
            number = ceil(random() * 10)
    #两位数
    if(countNumer==2 or number>99):
        while number<11:
           number=ceil(random()*100)
    #三位数
    if(countNumer==3 or number>999):
        while number < 101:
            number = ceil(random() * 1000)
    return number

          再写一个组装乘法算式的函数

#乘法算式
def multiFomula():
    firstNumber = randomNumber(2)
    secondNumber = randomNumber(2)
    numberStr = str(firstNumber) + "×" + str(secondNumber) + "="
    return numberStr

  写一个组装除法算式的函数:

#除法算式
def divFomula():
    firstNumber = randomNumber(3)
    secondNumber = randomNumber(1)
    numberStr = str(firstNumber) + "÷" + str(secondNumber) + "="
    return numberStr

创建一张PPT,并写明三年级计算题练习。

    prs=Presentation()
    title_slide_layout=prs.slide_layouts[0]            #布局模版0
    slide=prs.slides.add_slide(title_slide_layout)     
    title=slide.shapes.title
    subtitle=slide.placeholders[1]
    title.text="数学计算题练习"
    subtitle.text="三年级"

创建一个循环块,想要多少张PPT,这里生成10张即可:

    pageNumer=0
    while pageNumer<10:
        slide=prs.slides.add_slide(prs.slide_layouts[6])     #添加空白PPT

        #PPT添加文本框操作,往下看

        pageNumer+=1
        #文本框位置及大小参数,用Inches,需要导入from pptx.util import Inches
         text_box = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(2), Inches(0.4))
         text_box.text = "一、竖式计算"

        text_box = slide.shapes.add_textbox(Inches(1), Inches(1.5), Inches(1), Inches(0.5))
        text_box.text = multiFomula()

        text_box = slide.shapes.add_textbox(Inches(4), Inches(1.5), Inches(1), Inches(0.5))
        text_box.text = multiFomula()

        text_box = slide.shapes.add_textbox(Inches(7), Inches(1.5), Inches(1), Inches(0.5))
        text_box.text = divFomula()

        text_box = slide.shapes.add_textbox(Inches(1), Inches(3), Inches(1), Inches(0.5))
        text_box.text = multiFomula()

        text_box = slide.shapes.add_textbox(Inches(4), Inches(3), Inches(1), Inches(0.5))
        text_box.text = multiFomula()

        text_box = slide.shapes.add_textbox(Inches(7), Inches(3), Inches(2), Inches(0.5))
        text_box.text = divFomula() + "    (验算)"

        大功告成,记得保存。写代码的时候,有一个困惑没找到解决方法就是,text_box不知道怎么直接修改字体的大小和颜色等。网上的方案都是,用textframe,但是效果并不让人满意,麻烦明白的朋友告知一声,感谢~

  prs.save("三年数学练习题.pptx")

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号