前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python统计多个Powerpoint文件中幻灯片总数量

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

作者头像
Python小屋屋主
发布2018-04-16 17:15:06
1.5K0
发布2018-04-16 17:15:06
举报
文章被收录于专栏:Python小屋Python小屋

晚上吃饭时突然想知道自己做了多少页《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)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档