3
votes

Computing the sum page count of all my .docx, .doc, .ppt, .pptx, and .pdf files within a certain directory; but am a little confused at how to count PowerPoint slides.

Here's what I've tried:

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

Additionally I have tried using python-pptx however I received lxml errors (so tried to build my own lxml; which errored out on iconv dependency trouble). Also since that only supports pptx, I would need to find an alternative method for ppt. PowerPoint 2013 x64 is installed, and I am using Python 2.7.4 x64.

How do I get total slide count from PowerPoint presentations using Python?

2
Tried Presentation = Application.Presentations.Open(curr) but that gave me this error: File "<COMObject <unknown>>", line 3, in Open pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024773), None)A T

2 Answers

2
votes

Okay, figured out the answer.

It doesn't seem to like relative paths.

Adding this line to that function solves the problem:

from os import getcwd

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

I think most easiest way is this. In this way i can get total slide number.

from pptx import Presentation
prs = Presentation("path/example.pptx")
print(len(prs.slides))