Python如何统计PowerPoint演示文稿的幻灯片数?

3
计算指定目录下所有我的 .docx.doc.ppt.pptx.pdf 文件的总页数;但是我有点困惑如何计算幻灯片的页数。
以下是我的尝试:
from glob import glob
from PyPDF2 import PdfFileReader
import win32com.client

def pdf_page_count(filename):
    curr = open(filename, "rb")
    page_count = PdfFileReader(curr).getNumPages()
    curr.close()
    return page_count

def presentation_slide_count(filename):
    Application = win32com.client.Dispatch("PowerPoint.Application")
    Presentation = Application.Presentations.Open(filename)
    slide_count = len(Presentation.Slides)
    Presentation.Close()
    return slide_count

if __name__=='__main__':
    powerpoints = glob('*/*/*.pptx') + glob('*/*/*.ppt')
    documents = glob('*/*/*.docx') + glob('*/*/*.doc')
    pdf = glob('*/*/*.pdf')

    total_pdf_pages = sum([pdf_page_count(pdf) for pdf in pdf])
    total_docx_pages = 0
    total_powerpoint_slides = sum([presentation_slide_count(presentation)
                                   for presentation in powerpoints])

    print total_pdf_pages
    print total_powerpoint_slides

此外,我尝试使用python-pptx,但是我遇到了lxml错误(因此尝试构建自己的lxml;但由于iconv依赖问题而出现错误)。另外,由于它只支持pptx,我需要找到另一种针对ppt的替代方法。PowerPoint 2013 x64已安装,并且我正在使用Python 2.7.4 x64。
如何使用Python从PowerPoint演示文稿中获取幻灯片总数?

尝试使用Presentation = Application.Presentations.Open(curr),但是出现了以下错误:File "<COMObject <unknown>>", line 3, in Open pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024773), None) - A T
2个回答

2

好的,我已经找到答案了。

似乎不支持相对路径。

将这行代码添加到该函数中即可解决问题:

from os import getcwd

filename = getcwd() + '//' + filename

1
我认为最简单的方法是这样的。通过这种方式,我可以获得幻灯片的总数。
from pptx import Presentation
prs = Presentation("path/example.pptx")
print(len(prs.slides))

1
7年前,当我尝试安装Python-pptx时遇到了问题。 - A T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接