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度

即可完成

相关推荐
码界筑梦坊3 分钟前
116-基于Flask的健身房会员锻炼数据可视化分析系统
python·信息可视化·数据分析·flask·毕业设计
wcy_10114 分钟前
QCoder智能生成Excel数据清洗与可视化代码
python·excel
财经资讯数据_灵砚智能1 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月2日
人工智能·python·信息可视化·自然语言处理·ai编程
skiy1 小时前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
2601_956139421 小时前
集团品牌全案公司哪家专业
大数据·人工智能·python
ouliten1 小时前
[Triton笔记1]核心概念
笔记·python·深度学习·triton
清水白石0081 小时前
生成器不是性能银弹:什么时候该用 `yield` 省内存,什么时候它会拖慢 Python 数据处理吞吐?
开发语言·python·原型模式
李松桃1 小时前
Python爬虫-实战
爬虫·python
观无1 小时前
Python读取excel并形成api接口案例
python·pandas·fastapi
alwaysrun1 小时前
Python之文档自动上传至飞书云盘
python·飞书·uploader·云盘