Пример #1
0
def PrintStats(filename):
    if sys.platform == 'win32':
        if not pythoncom.StgIsStorageFile(filename):
            print "The file is not a storage file!"
            return
        # Open the file.
        flags = storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE
        stg_ = pythoncom.StgOpenStorage(filename, None, flags)

        # Now see if the storage object supports Property Information.
        try:
            pss = stg_.QueryInterface(pythoncom.IID_IPropertySetStorage)
        except pythoncom.com_error:
            print "No summary information is available"
            return
        # Open the user defined properties.
        ps = pss.Open(FMTID_UserDefinedProperties)
        props = PIDSI_TITLE, PIDSI_SUBJECT, PIDSI_AUTHOR, PIDSI_CREATE_DTM
        data = ps.ReadMultiple(props)
        # Unpack the result into the items.
        title, subject, author, created = data
        print "Title:", title
        print "Subject:", subject
        print "Author:", author
        print "Created:", created.Format()
Пример #2
0
def structured_storage(filename):
    """Pick out info from MS documents with embedded
     structured storage(typically MS Word docs etc.)

    Returns a dictionary of information found
    """

    if not pythoncom.StgIsStorageFile(filename):
        return {}

    flags = storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE
    storage = pythoncom.StgOpenStorage(filename, None, flags)
    try:
        properties_storage = storage.QueryInterface(pythoncom.IID_IPropertySetStorage)
    except pythoncom.com_error:
        return {}

    property_sheet = properties_storage.Open(FMTID_USER_DEFINED_PROPERTIES)
    try:
        data = property_sheet.ReadMultiple(PROPERTIES)
    finally:
        property_sheet = None

    title, subject, author, created_on, keywords, comments, template_used, \
     updated_by, edited_on, printed_on, saved_on, \
     n_pages, n_words, n_characters, \
     application = data

    result = {}
    if title:
        result['title'] = title
    if subject:
        result['subject'] = subject
    if author:
        result['author'] = author
    if created_on:
        result['created_on'] = created_on
    if keywords:
        result['keywords'] = keywords
    if comments:
        result['comments'] = comments
    if template_used:
        result['template_used'] = template_used
    if updated_by:
        result['updated_by'] = updated_by
    if edited_on:
        result['edited_on'] = edited_on
    if printed_on:
        result['printed_on'] = printed_on
    if saved_on:
        result['saved_on'] = saved_on
    if n_pages:
        result['n_pages'] = n_pages
    if n_words:
        result['n_words'] = n_words
    if n_characters:
        result['n_characters'] = n_characters
    if application:
        result['application'] = application
    return result
Пример #3
0
def parse_msg_file(filepath):
    mapi.MAPIInitialize((mapi.MAPI_INIT_VERSION, 0))
    storage_flags = STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE
    storage = pythoncom.StgOpenStorage(filepath, None, storage_flags, None, 0)
    mapi_session = mapi.OpenIMsgSession()
    message = mapi.OpenIMsgOnIStg(mapi_session, None, storage, None, 0,
                                  mapi.MAPI_UNICODE)
    return message
Пример #4
0
 def _bind_to_filter(self, fileName):
     """
     See if the file is a structured storage file or a normal file
     and then return an ifilter interface by calling the appropriate bind/load function
     """
     if pythoncom.StgIsStorageFile(fileName):
         self.stg  = pythoncom.StgOpenStorage(fileName, None, storagecon.STGM_READ | storagecon.STGM_SHARE_DENY_WRITE)
         try:
             self.f = ifilter.BindIFilterFromStorage(self.stg)
         except pythoncom.com_error, e:
             if e[0] == -2147467262: # 0x80004002: # no interface, try the load interface (this happens for some MSoft files)
                 self.f = ifilter.LoadIFilter(fileName)
             else:
                 raise
Пример #5
0
def get_ifilter_for_file(filename, log=log):
    """
    Deal with structured storage file if possible.
    See http://msdn2.microsoft.com/en-us/library/aa380369.aspx
    """

    if pythoncom.StgIsStorageFile(filename):
        storage_init_flags = STGM_READ | STGM_SHARE_DENY_WRITE
        stg = pythoncom.StgOpenStorage(filename, None, storage_init_flags)
        try:
            filt = ifilter.BindIFilterFromStorage(stg)
        except pythoncom.com_error, e:
            if e[0] == -2147467262:
                filt = load_ifilter(filename, log=log)
            else:
                raise
Пример #6
0
    def readOfficeProperties(self, shellName):
        if not pythoncom.StgIsStorageFile(filename):
            self.abstract = 'not a Storage file'

        flags = storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE
        stg = pythoncom.StgOpenStorage(filename, None, flags)

        try:
            pss = stg.QueryInterface(pythoncom.IID_IPropertySetStorage)
        except pythoncom.com_error:
            self.abstract = ''
            return

        # open properties
        ps = pss.Open(FMTID_UserDefinedProperties)
        props = PIDSI_TITLE, PIDSI_SUBJECT, PIDSI_KEYWORDS
        self.title, self.abstract, keywordText = ps.ReadMultiple(props)
        self.keywords = string.split(keywordText, ',')
Пример #7
0
 def testSimple(self):
     with pytest.raises(
             pythoncom.com_error):
         pythoncom.StgOpenStorage("foo",
         None,
         0)
Пример #8
0
 def _getException(self):
     try:
         pythoncom.StgOpenStorage("foo", None, 0)
     except pythoncom.com_error as exc:
         return exc
     self.fail("Didn't get storage exception.")
Пример #9
0
def get_stats(path):
    """ Function returns author,title,subject,keywords,comments,category using 
    the COM interface on windows"""
    # this is code lifted from a message by [email protected]
    # I changed some things
    author = title = subject = keywords = comments = category = None
    try:
        #This is all MS stuff
        pssread = pythoncom.StgOpenStorageEx(
            path, storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE,
            storagecon.STGFMT_FILE, 0, pythoncom.IID_IPropertySetStorage)
    except:
        try:
            stg = pythoncom.StgOpenStorage(
                path, None,
                storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)

            pssread = stg.QueryInterface(pythoncom.IID_IPropertySetStorage)
        except:
            print("No extended storage")
        else:
            try:
                ps = pssread.Open(
                    pythoncom.FMTID_SummaryInformation,
                    storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)
            except:
                pass
            else:
                author,title,subject,keywords,comments = ps.ReadMultiple(\
                (storagecon.PIDSI_AUTHOR, storagecon.PIDSI_TITLE,
                storagecon.PIDSI_SUBJECT, storagecon.PIDSI_KEYWORDS,
                storagecon.PIDSI_COMMENTS) )
            try:
                ps = pssread.Open(
                    pythoncom.FMTID_DocSummaryInformation,
                    storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)
            except:
                pass
            else:
                category = ps.ReadMultiple((storagecon.PIDDSI_CATEGORY, ))[0]
        stat_list = [author, title, subject, keywords, comments, category]
        stat_dictionary = dict([(Field, stat_list[index])
                                for index, Field in enumerate(GET_STATS_FIELDS)
                                ])
        return stat_dictionary
    else:
        try:
            ps = pssread.Open(
                pythoncom.FMTID_SummaryInformation,
                storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)
        except:
            pass
        else:
            author, title, subject, keywords, comments = ps.ReadMultiple(
                (storagecon.PIDSI_AUTHOR, storagecon.PIDSI_TITLE,
                 storagecon.PIDSI_SUBJECT, storagecon.PIDSI_KEYWORDS,
                 storagecon.PIDSI_COMMENTS))
        try:
            ps = pssread.Open(
                pythoncom.FMTID_DocSummaryInformation,
                storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)
        except:
            pass
        else:
            category = ps.ReadMultiple((storagecon.PIDDSI_CATEGORY, ))[0]
        try:
            ps = pssread.Open(
                pythoncom.FMTID_UserDefinedProperties,
                storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE)
        except:
            pass
        else:
            pass
        stat_list = [author, title, subject, keywords, comments, category]
        stat_dictionary = dict([(Field, stat_list[index])
                                for index, Field in enumerate(GET_STATS_FIELDS)
                                ])
        return stat_dictionary
Пример #10
0
 def _getException(self):
     try:
         pythoncom.StgOpenStorage("foo", None, 0)
     except pythoncom.com_error, exc:
         return exc