Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 days ago

migrated from superuser.com 2 days ago

1 Answer

Okay, figured out the answer.

It doesn't seem to like relative paths.

Adding this line to that function solves the problem:

filename = '[drive_letter]:\\[folder_path]' + filename
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.