Python's experience with win32com

Source: Internet
Author: User

        pYthon can use a third-party library called Win32com to achieve the purpose of operating COM, I was installed ActivePython third-party library, download the installation package from the official website, the third-party library almost encapsulates all the Win32 related operations under Python, such as Win32api,win32gui and so on, can be said to be relatively complete, you can Baidu to obtain.
The main is that there is a project may be used to convert PPT into a video function. later, when I want to use COM to manipulate Excel and Word,ppt, I believe most people, like me, are searching for Python how to manipulate ppt, or win32com to manipulate search statements such as Office .after the search is finished, point to go in, almost all directly is the code, I think so I foggy, if want to do some other operation can not do, I was the idea is to know these COM operation, exactly where can find to, because the online only a limited number of operations, injected into the PPT to add pictures , or Doc into a PDF, and the actual COM operation of office is more than just these functions, how do we know about the other APIs? In this way we can get rid of the code on the Internet and write our own COM code.
        a search, after Google, on a webpage finally found the answer:
Querying Interfaces

Now, how do does one investigate the detail of each object? For example, how does one access the name of a folder? Firstly, it helps to know the interface, the object exposes, and this information can is found in several places:

  • The Microsoft API documentation.
  • Other useful sources, such as the "Outlook Exchange" documentation.
  • The interface file generated by the "COM makepy utility". To know which file was relevant to the interface in question, either perform a "grep" search for the name of the interface On the Win32com/gen_py directory within your Python distribution, or invoke an erroneous method or access a non-existent a Ttribute on a object with this interface in order to see what the name of the interface file is.
  • the ' ole/com Object Viewer ' in the ' Tools ' menu in Microsoft Visual C + + (at least in Visual Studio 6.0).
    • Once opened, the "Interfaces" section of the information hierarchy can is explored to reveal some "_applicati On "entries. For one of these, the information pane would mention "Microsoft Outlook 9.0 Object Library" under "TypeLib" for example.
    • double-clicking on a "_application" entry would provide an "ITypeInfo Viewer" which contains a "_methods" se Ction describing the available methods on the application ' s automation object.
    • the "Type Libraries" section of the information hierarchy would list, for example, "Microsoft Outlook 9.0 Obj ECT Library ", and this can is investigated by double-clicking on the that entry.

Hopefully, however, the object that's accessing is known well enough by Pythonwin to permit some kind of attribute O R method completion on it. You should only need to resort to the above when more detailed knowledge about a method or attribute is required. Can also try something like this:

Dir (object.__class__)

The name of a folder can be accessed as follows:

Object. Name # Where object refers to a folder.
the fourth method here is that I found the confirmation valid, the other three if interested can try, the fourth method that is Ole/com Object Viewer tool, under Baidu, downloaded a tool like this, it is said to install the VS after the have, However, since I do not know the name of the executable program, there is no way to find, and then re-download a complete tool, after installation The default installation path is: C:\Program Files (x86) \resource Kit

I was installed when the point is too fast, the result forgot the path, re-click Install, note the path.

This tool name is called Oleview.exe, when opened, the hint is missing what DLL, it doesn't matter. because I now know the name, and then I searched for the tool using everything, and I found the tool in my Visual Studio, which is perfect to open. the software interface looks like this:

Remember to be on the right side of the

Type libraries inside find the relevant library, here I need to operate is PowerPoint, that is PPT
once found, double-click to open it. on the right is a list, the left side is for the content, just opened, the left side of the display is the full PowerPoint API. Because of this tool, not able to ctrl+f find, we can ctrl + A, copy the contents of the left into the text, use other such as sublime text editor to perform the Find function. Here's a look: saveAs (probably what this means, I want to find an API can be saved as PPT for video operation)


we found this function, together with the example of the Internet, we know how to use, the first parameter passed in is filename, as the name implies is the file name, the second is an int type of FileFormat, if it is an example on the Internet, I'll probably just tell you a code that's converted to PDF, but now I want to turn it into a video. let's go back to the OLE viewer and see if there's fileformat information. Sure enough, we found this code:
PpSaveAsFileType Double-click it


in the end, I found the ppsaveaswmv, very good, this way we can combine online examples, modified.

Now the method of operation PPT We understand, then the operation of Word,excel is the same truth.

__author__ = ' zxc ' import win32com.clientimport timeimport osppsaveaswmv = 37# only for Windows platform and with the micro Soft office or Above,it needs the library win32comdef cover_ppt_to_wmv (ppt_src,wmv_target):    ppt = Win32com.clien T.dispatch (' powerpoint.application ')    presentation = ppt. Presentations.Open (ppt_src,withwindow=false)    presentation. Createvideo (wmv_target,-1,4,720,24,60)    Start_time_stamp = Time.time () while    True:        time.sleep (4)        Try:            os.rename (wmv_target,wmv_target)            print ' success '            break        except Exception, E:            pass    End_time_stamp=time.time ()    print End_time_stamp-start_time_stamp    ppt. Quit ()    passif __name__ = = ' __main__ ':    cover_ppt_to_wmv (' d:\\python\\demo.ppt ', ' d:\\python\\demo.wmv ')






Python's experience with win32com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.