DKProthro
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.Appl ication")
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!
import sys, win32com.client
doc = sys.argv[1]
try:
visio = win32com.client.Dispatch("
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!
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.getctim e('c:\\tes t.doc'))
print "Time modified: " + time.ctime(os.path.getatim e('c:\\tes t.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
For example:
-----
import os
import time
print "Time created: " + time.ctime(os.path.getctim
print "Time modified: " + time.ctime(os.path.getatim
-----
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
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
import sys, win32com.client
doc = sys.argv[1]
try:
mpp = win32com.client.Dispatch("
mpp.Visible = 1
try:
mpp.FileOpen(doc)
proj = mpp.ActiveProject
print proj.BuiltinDocumentProper
except Exception, e:
print "Error", e
mpp.FileSave()
mpp.Quit()
except Exception, e:
print "Error opening file",e