python Win32 module

1. Preparation:

       pywin32 module  download address

        Jython  download address

Note: When downloading the pywin32 module, you need to choose the appropriate one, first according to the python version, and then refer to whether python is 32-bit or 64-bit



Since it is a 32-bit python 2.7.14, choose pywin32-223.win32-py2.7.exe

In addition, when downloading Jython, it is guaranteed to have jdk. The corresponding relationship is that Jython2.5 requires jdk5 or above, and Jython2.6 requires jdk6 or above.


In the test on my own machine (jdk8 has been installed), I found that there is no jython.bat file in the directory after installation in Jython 2.7.0, so it cannot be run. It is recommended not to choose Jython 2.7.0. After comprehensive consideration, choose Jython 2.5.2 .

Install jython2.5.2, configure environment variables, add the Jython installation directory to path. Then cmd enter jython to enter the interactive interface. However, when you enter print "hello world", the string result is not displayed, and it is still waiting for input. That is, the display after a newline is..., or when performing a standard operation, a jython LookupError is reported. Please refer to the jython console code.


2. Interact with Excel

# -*- coding: cp936 -*-
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning#The above three are to use the message box to demonstrate the process
import win32com.client as win32

warn=lambda app:showwarning(app,'Exit?')#Show message
RANGE=range(3,8)#loop print

def excel():
    app='Excel'
    x1=win32.gencache.EnsureDispatch('%s.Application' %app)#Static enable
    ss=x1.Workbooks.Add()#Add workbook
    sh=ss.ActiveSheet#current sheet
    x1.Visible=True#Visible
    sleep(1)

    sh.Cells(1,1).Value='Python-to-%s Demo' % app#Write data in 1 row and 1 column
    sleep(1)
    for i in RANGE:
        sh.Cells(i,1).Value='Line %d' % i
        sleep(1)
    sh.Cells(i+2,1).Value="Th-th-th-that's all folks!"

    warn(app)#Message box
    ss.Close(False)#Do not save and leave
    x1.Application.Quit()#Exit the program

if __name__=='__main__':
    Tk().withdraw()#Draw the topmost window first, otherwise the system will automatically create one and keep it on the screen
    excel()

After running:



3. Interact with word

# -*- coding: cp936 -*-
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning#The above three are to use the message box to demonstrate the process
import win32com.client as win32

warn=lambda app:showwarning(app,'Exit?')
RANGE=range(3,8)#loop print

def excel():
    app='Word'
    word=win32.gencache.EnsureDispatch('%s.Application' %app)#Static enable
    doc=word.Documents.Add()#Add document
    word.Visible=True#Visible
    sleep(1)

    rng=doc.Range(0,0)#Cursor position
    rng.InsertAfter('Python-to-%s Test\r\n\r\n' % app)#Insert
    sleep(1)
    for i in RANGE:
        rng.InsertAfter('Line %d\r\n' % i)
        sleep(1)
    rng.InsertAfter("\r\nTh-th-th-that's all folks!\r\n")

    warn(app)#Message box
    doc.Close(False)#Do not save and leave
    word.Application.Quit()#Exit the program

if __name__=='__main__':
    Tk().withdraw()#Draw the topmost window first, otherwise the system will automatically create one and keep it on the screen
    excel()

After running:


For ppt and outlook, because of the running error, it is no longer displayed here.


It is assumed that the downloaded pywin32 module does not match. Note that the suffix of the file name above is saved as .pyw as well


4. Jython

When Jython is configured, many packages in Java can be used, such as java.awt

from pawt import swing
import sys
from java.awt import Color,BorderLayout

def quit(e):
    sys.exit(0)

top=swing.JFrame("PySwing")
box=swing.JPanel()
hello=swing.JLabel("Hello World")#标签
quit=swing.JButton("QUIT",actionPerformed=quit,background=Color.red,foreground=Color.white)#Button, register events and add colors

box.add("North",hello)
box.add("South",quit)
top.contentPane.add(box)#Layout
top.pack()#Automatically set the appropriate size
top.visible=1#Visible

Open cmd, enter the directory where the file is located, and use jython abc.py. Result:



V. Summary

    Basically, many languages ​​can interact with excel, word, etc. For example, the functions that come with mathlab, it is really powerful to operate excel with matlab matrices and scientific computing; and java POI, java can easily implement various complicated operations And it is completely object-oriented, with high efficiency and good stability, and can achieve good interaction; python's pywin32 module also realizes the interaction of components, python is easy to understand and has strong expressive ability, and has advantages in automatic control and system management, as well as the characteristics of the scripting language itself It is very large, using python to achieve interaction, and mastering it is very suitable for combining with excel, word and other office software.

    Jython is a great tool, both the various apis of java and the automatic function library of Python. Besides that, Jython runs faster than python, the default python is Cpython. Jython can make the job easier.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325647609&siteId=291194637