1 Star 1 Fork 2

Necho/PPTxPython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

基于VBA简单实现PPT嵌入式运行Python程序

效果图

运行截图

实现思路

实现思路

运行环境

  1. MS Office PowerPoint (未禁用VBA)
  2. 已部署所需的Python运行环境

功能预想

可满足Python教学工作者的日常基础Python课程教学,即在放映PowerPoint时可以直接运行Python代码并显示代码运行结果,使课程教学过程更加高效、便捷!已支持中文utf-8编码

代码实现

单击“运行Python代码”按钮
Private Sub CommandButton1_Click()
    
    '写入代码
    Dim f, txt, result As String
    f = "run.py"
    Open f For Output As #1
    Print #1, TextBox_in.Value
    Close #1
    
    '中文编码问题,需将文件转为utf-8编码格式
    Dim fileName As String
    fileName = "run.py"
    Call ConvFile(fileName, fileName)
    
    '运行Python文件并重定向结果
    Dim res As Long
    res = Shell("cmd.exe /C python " & "run.py > res.txt", vbHide)
    
    '延时处理
    Dim Savetime As Single
    CommandButton1.Caption = "运行中..."
    Savetime = Timer '记下开始的时间
    While Timer < Savetime + 1 '循环等待
    DoEvents '转让控制权,以便让操作系统处理其它的事件
    Wend
    CommandButton1.Caption = "运行Python代码"
    
    '读取结果
    Open "res.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, txt
        result = result & txt & vbCr
    Loop
    Close #1
    TextBox_out.Text = result
End Sub
转换文件编码为“utf-8”,解决中文编码问题
Private Sub ConvFile(InputFile As String, OutputFile As String) 
    Dim ReadStream As Object
    Set ReadStream = CreateObject("ADODB.Stream")
    Dim FileContent As String
    With ReadStream
        .Type = 2               'adTypeText
        '.Charset = "UNICODE"
        .Charset = "GB2312"     ' ANSI
        '.Charset = "UTF-8"
        .Open
        .LoadFromFile InputFile
        FileContent = .ReadText
        .Close
    End With
    Set ReadStream = Nothing
    Dim WriteStream As Object
    Set WriteStream = CreateObject("ADODB.Stream")
    With WriteStream
        .Type = 2               'adTypeText
        .Charset = "UTF-8"
        .Open
        .WriteText FileContent
        .SaveToFile OutputFile, 2  'adSaveCreateOverWrite
        .Flush
        .Close
    End With
    Set WriteStream = Nothing
End Sub

现存缺陷

由于个人能力有限,目前仅支持对于简单基础的Python代码的嵌入式运行,无法满足带有输入的代码(如input()等)以及一些调用复杂库的代码输出。只能满足日常基础Python课程的需要

关于作者:

学生 计算机科学与技术本科在读 前端初学者

WeChat赞赏

微信公众号:浙江技术救命稻草

喜欢请记得关注哦

MIT License Copyright (c) 2021 Eliauk /**基于VBA简单实现PPT嵌入运行Python程序**/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于VBA简单实现PPT嵌入运行Python程序 - 可满足Python教学工作者的日常基础Python课程教学,即在放映PowerPoint时可以直接运行Python代码并显示代码运行结果,使课程教学过程更加高效、便捷!已支持中文 UTF-8编码 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Visual Basic
1
https://gitee.com/necho-dev/pptx-python.git
git@gitee.com:necho-dev/pptx-python.git
necho-dev
pptx-python
PPTxPython
master

搜索帮助