python + PPT

ppt转化为word

对于PPT中的文本内容,如何转化为word内容呢?下面的代码可以实现如下功能

代码如下:

python 复制代码
from pptx import Presentation
from docx import Document

def clean_text(text):
    """清理文本,移除控制字符"""
    # 移除NULL字节和控制字符
    return ''.join(ch for ch in text if ch == '\n' or ch == '\r' or ch.isprintable())

def extract_text_from_shape(shape):
    """从形状中提取文本"""
    if hasattr(shape, 'text'):
        return clean_text(shape.text)
    elif hasattr(shape, 'text_frame'):
        text = []
        for paragraph in shape.text_frame.paragraphs:
            # 清理每个段落的文本
            for run in paragraph.runs:
                text.append(clean_text(run.text))
        # 合并段落,并用换行符分隔
        return '\n'.join(text)
    return ""

def ppt_to_docx(ppt_file, docx_file):
    # 加载PPT文件
    prs = Presentation(ppt_file)
    
    # 创建一个新的Word文档
    doc = Document()
    
    # 遍历PPT中的每个幻灯片
    for slide_number, slide in enumerate(prs.slides):
        # 遍历幻灯片中的每个形状
        for shape in slide.shapes:
            text = extract_text_from_shape(shape)
            if text:  # 只有当文本非空时才添加
                doc.add_paragraph(text)
    
    # 保存Word文档
    doc.save(docx_file)

# 使用函数
ppt_file = 'mytest.pptx'  # 替换为你的PPT文件路径,文件名可以定位mytest.pptx
docx_file = 'output_word_file.docx'  # 输出的Word文件名
ppt_to_docx(ppt_file, docx_file)

即可完成。

一日一画

python 复制代码
import turtle
turtle.pensize(1)



for i in range(36):  # 循环36次,每次增加角度和长度
    turtle.forward(i * 10)  # 每次前进的距离逐渐增加
    turtle.right(144)  # 右转144度

即可完成

相关推荐
孟健13 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞15 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽17 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python