Attaching files in windows using Python.

Discussion in 'Python' started by sri2097, Mar 7, 2006.

  1. sri2097

    sri2097 Guest

    Hi all,
    I have to select a particular file (using the 'Browse') button in
    Windows. After this I need to populate the 'Open Dialogue Box' with the
    path of the file I need (I have the entier path of the file I need).
    Then I need to select the 'Open' Button.

    Only after this the file I want is attached.

    Any idea as to how this can be done using 'Win32 API's.

    While looking for the proper answer to this I found that 'Mark
    Hammond's' Python for Windows documentation is not detail enough.

    Any help in this regard would be much appreciated.
    sri2097, Mar 7, 2006
    #1
    1. Advertising

  2. sri2097

    sri2097 Guest

    Hi all,
    I have got this far till now -

    import win32gui, struct, array, string

    OFN_ALLOWMULTISELECT=0x00000200
    OFN_EXPLORER=0x00080000

    def arrayToStrings(resultArray):
    """return list-of-strings corresponding to a char array,
    where each string is terminated by \000, and the whole
    list by two adjacent \000 bytes
    """
    astr=resultArray.tostring()
    manyStrings=[]
    # perhaps a loop of string.split would be faster...
    while len(astr) and astr[0]!='\000':
    i=astr.index('\000')
    manyStrings.append(astr[:i])
    astr=astr[i+1:]
    return manyStrings

    def szFrom(anarray):
    """return the string-pointer (sz) corresponding to a char
    array, 0 (null pointer) if no array
    """
    if anarray: return anarray.buffer_info()[0]
    else: return 0

    def arrayFrom(astring,additional=0):
    """return a char array built from a string, plus 0
    or more \000 bytes as filler
    """
    if not astring: astring=''
    return array.array('c',astring+additional*'\000')

    def arrayMulti(stringlist):
    """return a char array built from many strings, each
    separated by a \000 byte, and two \000's at the end
    """
    return arrayFrom(string.join(stringlist,'\000'),2)

    def buildOfn(resultarray,filters=None,initdir=None,title=None,
    multisel=1,oldlook=0):
    """build an OPENFILENAME struct as a string, with several
    options and a given result-array for the string that
    will result from the GetOpenFileName call
    """
    flags=OFN_EXPLORER
    if multisel: flags=flags|OFN_ALLOWMULTISELECT
    if oldlook: flags=flags&~OFN_EXPLORER
    szfile,maxfile=resultarray.buffer_info()
    szfilter=szFrom(filters)
    szinitdir=szFrom(initdir)
    sztitle=szFrom(title)
    return struct.pack(
    "3i2P2iPiPi2PI2hPi2P",
    76, 0, 0, # size, owner-hwnd, hinstance
    szfilter, 0, 0, 0, # filter, custom-filter,
    max-cust-filter,filter-index
    szfile, maxfile, # file, max-file
    0, 0, # file-title, max-file-title
    szinitdir, sztitle, # initial-dir, dialog-title
    flags, 0, 0, # flags, file-offset, file-extension
    0, # def-ext
    0, 0, 0) # cust-data, func-hook, template-name

    def openNames(forsave=0,filters=None,initdir=None,title=None,
    initfile=None,multisel=1,oldlook=0):
    """return a list of filenames for open or save, given
    interactively by the user through a common-dialog; if
    more than 1 string is returned, the first is the directory,
    followed by the filenames.
    """
    resultBuffer=arrayFrom(initfile,8192)
    title=arrayFrom(title)
    initdir=arrayFrom(initdir)
    filters=arrayMulti(filters)
    ofn=buildOfn(resultBuffer,filters,initdir,title,multisel,oldlook)
    if forsave: isok=win32gui.GetSaveFileName(ofn)
    else: isok=win32gui.GetOpenFileName(ofn)
    if not isok: return []
    return arrayToStrings(resultBuffer)

    def _test():
    return openNames(
    filters=('Texts and scripts','*.txt;*.py','Py stuff','*.py*')
    )

    if __name__=='__main__':
    print _test()

    But hear the Dialogue_box stops and waits for the user to select a
    file. But Since I have the entire path of the file, How do I pass it to
    the file name to populate the box automatically instead of the user
    manually selecting a file.

    Any further help will be appreciated.
    sri2097, Mar 7, 2006
    #2
    1. Advertising

  3. sri2097

    Roger Upole Guest

    You might want to try using win32gui.GetOpenFileNameW.
    It uses keyword arguments and doesn't require that you
    build a struct yourself:

    win32gui.GetOpenFileNameW(File='myfile.txt', Filter='Texts and scripts\0*.txt;*.py\0Py stuff\0*.py\0')

    Roger


    "sri2097" <> wrote in message news:...
    > Hi all,
    > I have got this far till now -
    >
    > import win32gui, struct, array, string
    >
    > OFN_ALLOWMULTISELECT=0x00000200
    > OFN_EXPLORER=0x00080000
    >
    > def arrayToStrings(resultArray):
    > """return list-of-strings corresponding to a char array,
    > where each string is terminated by \000, and the whole
    > list by two adjacent \000 bytes
    > """
    > astr=resultArray.tostring()
    > manyStrings=[]
    > # perhaps a loop of string.split would be faster...
    > while len(astr) and astr[0]!='\000':
    > i=astr.index('\000')
    > manyStrings.append(astr[:i])
    > astr=astr[i+1:]
    > return manyStrings
    >
    > def szFrom(anarray):
    > """return the string-pointer (sz) corresponding to a char
    > array, 0 (null pointer) if no array
    > """
    > if anarray: return anarray.buffer_info()[0]
    > else: return 0
    >
    > def arrayFrom(astring,additional=0):
    > """return a char array built from a string, plus 0
    > or more \000 bytes as filler
    > """
    > if not astring: astring=''
    > return array.array('c',astring+additional*'\000')
    >
    > def arrayMulti(stringlist):
    > """return a char array built from many strings, each
    > separated by a \000 byte, and two \000's at the end
    > """
    > return arrayFrom(string.join(stringlist,'\000'),2)
    >
    > def buildOfn(resultarray,filters=None,initdir=None,title=None,
    > multisel=1,oldlook=0):
    > """build an OPENFILENAME struct as a string, with several
    > options and a given result-array for the string that
    > will result from the GetOpenFileName call
    > """
    > flags=OFN_EXPLORER
    > if multisel: flags=flags|OFN_ALLOWMULTISELECT
    > if oldlook: flags=flags&~OFN_EXPLORER
    > szfile,maxfile=resultarray.buffer_info()
    > szfilter=szFrom(filters)
    > szinitdir=szFrom(initdir)
    > sztitle=szFrom(title)
    > return struct.pack(
    > "3i2P2iPiPi2PI2hPi2P",
    > 76, 0, 0, # size, owner-hwnd, hinstance
    > szfilter, 0, 0, 0, # filter, custom-filter,
    > max-cust-filter,filter-index
    > szfile, maxfile, # file, max-file
    > 0, 0, # file-title, max-file-title
    > szinitdir, sztitle, # initial-dir, dialog-title
    > flags, 0, 0, # flags, file-offset, file-extension
    > 0, # def-ext
    > 0, 0, 0) # cust-data, func-hook, template-name
    >
    > def openNames(forsave=0,filters=None,initdir=None,title=None,
    > initfile=None,multisel=1,oldlook=0):
    > """return a list of filenames for open or save, given
    > interactively by the user through a common-dialog; if
    > more than 1 string is returned, the first is the directory,
    > followed by the filenames.
    > """
    > resultBuffer=arrayFrom(initfile,8192)
    > title=arrayFrom(title)
    > initdir=arrayFrom(initdir)
    > filters=arrayMulti(filters)
    > ofn=buildOfn(resultBuffer,filters,initdir,title,multisel,oldlook)
    > if forsave: isok=win32gui.GetSaveFileName(ofn)
    > else: isok=win32gui.GetOpenFileName(ofn)
    > if not isok: return []
    > return arrayToStrings(resultBuffer)
    >
    > def _test():
    > return openNames(
    > filters=('Texts and scripts','*.txt;*.py','Py stuff','*.py*')
    > )
    >
    > if __name__=='__main__':
    > print _test()
    >
    > But hear the Dialogue_box stops and waits for the user to select a
    > file. But Since I have the entire path of the file, How do I pass it to
    > the file name to populate the box automatically instead of the user
    > manually selecting a file.
    >
    > Any further help will be appreciated.
    >




    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Roger Upole, Mar 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJpYW4gRmFybmhpbGwgKE1DUCBWQi5ORVQp?=

    Error when attaching multiple files (System.web.mail)

    =?Utf-8?B?QnJpYW4gRmFybmhpbGwgKE1DUCBWQi5ORVQp?=, Apr 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    556
    =?Utf-8?B?QnJpYW4gRmFybmhpbGwgKE1DUCBWQi5ORVQp?=
    Apr 19, 2005
  2. wal

    libgmail: attaching files

    wal, Oct 4, 2006, in forum: Python
    Replies:
    0
    Views:
    273
  3. Replies:
    1
    Views:
    129
    Martin Kulov
    Feb 17, 2006
  4. Tapiwa JONGWE

    Attaching files in ASP

    Tapiwa JONGWE, Jul 28, 2003, in forum: ASP General
    Replies:
    1
    Views:
    103
    Tapiwa JONGWE
    Jul 28, 2003
  5. knocte
    Replies:
    4
    Views:
    162
Loading...

Share This Page