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度

即可完成

相关推荐
不只会拍照的程序猿26 分钟前
《嵌入式AI筑基笔记02:Python数据类型01,从C的“硬核”到Python的“包容”》
人工智能·笔记·python
Jay_Franklin35 分钟前
Quarto与Python集成使用
开发语言·python·markdown
Oueii1 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
2401_831824962 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf2 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
@我漫长的孤独流浪2 小时前
Python编程核心知识点速览
开发语言·数据库·python
宇擎智脑科技2 小时前
A2A Python SDK 源码架构解读:一个请求是如何被处理的
人工智能·python·架构·a2a
2401_851272992 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
vx_biyesheji00012 小时前
Python 全国城市租房洞察系统 Django框架 Requests爬虫 可视化 房子 房源 大数据 大模型 计算机毕业设计源码(建议收藏)✅
爬虫·python·机器学习·django·flask·课程设计·旅游
code 小楊2 小时前
yrb 1.5.0 正式发布:Python 极简国内下载加速与全景可视化终端体验!
开发语言·python