Link to home
Start Free TrialLog in
Avatar of DKProthro
DKProthroFlag for United States of America

asked on

win32com with Common file types

I'm writing a series of functions to grab the Date Created field for different file types. I've been successful for Word, Excel, Powerpoint and Visio. I'm currently trying to write a function for MS Project, but am getting stuck. I need a program that will open an MS Project file, find the Date/Time it was created and then close the file. My code for Visio is as follows:

import sys, win32com.client

doc = sys.argv[1]

try:
      visio = win32com.client.Dispatch("Visio.Application")
      visio.Visible = 0
      try:
            dwg = visio.Documents.Open(doc)
            print dwg.TimeCreated, ",", dwg.TimeSaved
      except Exception, e:
            print "Error", e

      dwg.Close()
      visio.Quit()
except Exception, e:
      print "Error opening file",e
 
I would also greatly appreciate any ideas on how to do something similar for PDF and other common filetypes (I haven't started looking at this yet). Thanks!
Avatar of DKProthro
DKProthro
Flag of United States of America image

ASKER

I found a solution for MS Project, but now I need solutions for non-MS file types (PDF, images, movies, ZIP). My code for MS Project is:

import sys, win32com.client

doc = sys.argv[1]

try:
      mpp = win32com.client.Dispatch("MSProject.Application")
      mpp.Visible = 1
      try:
            mpp.FileOpen(doc)
            proj = mpp.ActiveProject
            print proj.BuiltinDocumentProperties(11), ",", proj.BuiltinDocumentProperties(12)
      except Exception, e:
            print "Error", e

      mpp.FileSave()
      mpp.Quit()
except Exception, e:
      print "Error opening file",e
Avatar of BruceDWebber
BruceDWebber

Rather than look inside the file for this information, you can get this information from the operating system. The OS stores the create time, access time and the time modified, as seconds since the start of the Epoch. Python has functions, in the time module, for converting this number into time structures and strings. This works regardless of the file type.

For example:

-----

import os
import time

print "Time created: " + time.ctime(os.path.getctime('c:\\test.doc'))
print "Time modified: " + time.ctime(os.path.getatime('c:\\test.doc'))

-----

getctime returns the create time, as the number of seconds since the start of the Epoch. Similarly, getatime returns the access time and getmtime the time modified. time.ctime converts this into a string, but you can also convert these into a time structure that will let you access the year, month, day, etc. separately.

Here's the relevant Python documentation:
http://www.python.org/dev/doc/devel/lib/module-os.path.html for os.path
http://www.python.org/doc/2.3/lib/module-time.html for time

Regards,
Bruce
Sorry, I should have gone into more detail about what I'm looking for. The files I'm interested in are uploaded to a server, so the OS time the server shows is really just the upload time, not the actual file-creation date. Most filetypes store the actual file creation date in their metadata, its just a matter of getting at it.

So far, I've been successful with Microsoft's file-types, but not with any others. Using COM is a solution I think will work, I'm just not totally sure how to do it. Any other solutions are welcome though.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial