Python统计多个Powerpoint文件中幻灯片总数量

晚上吃饭时突然想知道自己做了多少页《Python程序设计》系列教材的配套PPT,于是就有了下面的代码,这套PPT综合了《Python程序设计基础》(ISBN:9787302410584)、《Python程序设计(第2版)》(ISBN:9787302436515)和《Python可以这样学》(ISBN:9787302456469)以及将要出版的《Python程序设计开发宝典》4本书的内容,部分内容比书上详细,有的地方不如书上详细,主要是上课用,几本书重点介绍Python 3.4.x、3.5.x、3.6.x的语法和应用,全套课件均已免费分享。

import os
import os.path
import win32com
import win32com.client


total = 0


def pptCount(path):
    global total
    for subPath in os.listdir(path):
        subPath = os.path.join(path, subPath)
        if os.path.isdir(subPath):
            pptCount(subPath)
        elif subPath.endswith('.ppt'):
            print(subPath)
            powerpoint = win32com.client.Dispatch('PowerPoint.Application')
            powerpoint.Visible = 1
            ppt = powerpoint.Presentations.Open(subPath)
            win32com.client.gencache.EnsureDispatch('PowerPoint.Application')
            total += ppt.Slides.Count
            powerpoint.Quit()


pptCount('F:\\教学课件\\Python程序设计(第二版)')
print(total)


运行结果显示:

pptx肿么办?

首先:

pip install python-pptx

然后:

>>> import pptx
>>> p = pptx.Presentation('f:\\1.pptx')
>>> len(p.slides)
3

另外,关于昨天发的文章再补充一下,原文参见Python计算序列中数字最大差值(美团2016校招笔试题)

昨天发文之后立刻有上海交大李老师和读者朋友zhouyonghaha指出算法效率太低,其实一次循环就可以,开始我还不太明白,想了一下果然如此,于是有了下面的高效代码,算作一个补充:

from random import randrange


def maxDifference2(lst):
    diff = -float('inf')    
    minCurrent = lst[0]    
    for value in lst[1:]:
        if value < minCurrent:
            minCurrent = value
        else:
            t = value-minCurrent
            if t > diff:
                diff = t
                result = (minCurrent, value)
    return result


for _ in range(10):
    print('='*20)
    lst = [randrange(1,100) for _ in range(20)]
    print(lst)
    print(maxDifference2(lst))


运行结果显示:

====================
[22, 20, 32, 66, 22, 74, 74, 31, 88, 94, 18, 35, 47, 75, 14, 83, 44, 57, 53, 95]
(14, 95)
====================
[60, 15, 46, 36, 93, 45, 92, 56, 36, 57, 87, 80, 47, 4, 72, 18, 79, 32, 35, 1]
(15, 93)
====================
[4, 40, 92, 99, 87, 14, 52, 55, 35, 52, 1, 53, 50, 46, 39, 53, 29, 8, 45, 32]
(4, 99)
====================
[41, 53, 52, 47, 93, 67, 18, 38, 77, 12, 87, 42, 43, 2, 16, 32, 20, 54, 33, 72]
(12, 87)
====================
[68, 41, 29, 33, 23, 81, 5, 41, 17, 54, 69, 29, 90, 10, 57, 88, 14, 30, 69, 81]
(5, 90)
====================
[1, 46, 27, 47, 68, 44, 89, 15, 2, 10, 32, 90, 45, 79, 33, 99, 21, 61, 79, 21]
(1, 99)
====================
[64, 58, 97, 65, 15, 13, 35, 86, 25, 58, 26, 51, 65, 14, 6, 98, 90, 25, 98, 42]
(6, 98)
====================
[55, 14, 18, 57, 40, 27, 55, 93, 21, 16, 48, 32, 93, 69, 50, 13, 89, 98, 59, 40]
(13, 98)
====================
[11, 19, 17, 96, 21, 25, 74, 71, 78, 8, 49, 58, 57, 36, 72, 56, 83, 93, 41, 65]
(11, 96)
====================
[48, 95, 56, 44, 42, 40, 83, 86, 82, 50, 73, 88, 98, 52, 28, 60, 33, 17, 68, 59]
(40, 98)

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dongfuguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值