Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


win32com with Common file types

Posted on 2005-03-30
Medium Priority
Last Modified: 2012-06-21
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]

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

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!
Question by:DKProthro
  • 2

Author Comment

ID: 13663850
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]

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

except Exception, e:
      print "Error opening file",e

Expert Comment

ID: 13679003
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


Author Comment

ID: 13682141
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.

Accepted Solution

Computer101 earned 0 total points
ID: 15711390
PAQed with points refunded (200)

EE Admin

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question