directory

 

Keyboard simulation

The mouse simulation

Read doc files and docx files

Creating a Word file

Read the XLSX file


Keyboard simulation

 

Example: Simulate pressing Windows in the lower left corner

import win32con
import win32api
import time

Press the WIN control key
win32api.keybd_event(91.0.0.0)
time.sleep(0.1)
win32api.keybd_event(91.0,win32con.KEYEVENTF_KEYUP,0)
Copy the code

Running results:

Example: Minimize all displayed Windows

Minimize all displayed items
while True:
    win32api.keybd_event(91.0.0.0)
    time.sleep(0.1)
    win32api.keybd_event(77.0.0.0)
    time.sleep(0.1)
    win32api.keybd_event(77.0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(91.0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(3)
Copy the code

 

The mouse simulation

Example: this is equivalent to pressing (30,40) twice in a row on the interface

import win32api
import win32con
import time

# Set mouse position
win32api.SetCursorPos([30.40])
time.sleep(0.1)
# Left mouse button down
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0.0.0)
# Raise the left mouse button
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0.0.0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0.0.0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0.0.0)
Copy the code

Running results:

 

 

Read doc files and docx files

Example: Reading doc files

import win32com
import win32com.client

def readWordFile(path) :
    # Call system word function, can handle doc and docXL two kinds of files
    mw = win32com.client.Dispatch('Word.Application')
    # Open file
    doc = mw.Documents.Open(path)
    # Remove each paragraph from the document
    for paragraph in doc.Paragraphs:
        line = paragraph.Range.Text
        print(line)
    # close file
    doc.Close()
    # out of word
    mw.Quit()

path = r'C:\Users\asus\Desktop\a.doc'
readWordFile(path)
Copy the code

Running results:

 

Creating a Word file

Example: Create three Word files

import win32com
import win32com.client
import os

def makeWordFile(filename,name) :
    word = win32com.client.Dispatch('Word.Application')
    Make the document visible
    word.Visible = True
    Create a document
    doc = word.Documents.Add()
    # write content
    # Write from scratch
    r = doc.Range(0.0)
    r.InsertAfter('hello'+name + '\n')
    r.InsertAfter('Wow... \n')
    # Save file
    doc.SaveAs(filename)
    # close file
    doc.Close()
    # out of word
    word.Quit()

filenames = ['Joe'.'bill'.'Cathy']
for name in filenames:
    filename = os.path.join(os.getcwd(),name)
    makeWordFile(filename,name)
Copy the code

Running results:

 

Read the XLSX file

To read XLSX files, you need to install an OpenPyXL package

The table file to prepare for reading

Example: Display the names of all tables

from openpyxl.reader.excel import load_workbook

def readXlsx(path) :
    # Open file
    file = load_workbook(filename=path)
    The names of all tables
    print(file.sheetnames)

path = r'C:\Users\asus\Desktop\e.xlsx'
readXlsx(path)
Copy the code

Running results:

Example: Reading a table

from openpyxl.reader.excel import load_workbook

def readXlsx(path) :
    # Open file
    file = load_workbook(filename=path)

    # Take out a table
    sheet = file.worksheets[0]
    # maximum number of rows
    print(sheet.max_row)
    # maximum number of columns
    print(sheet.max_column)
    # the name of the table
    print(sheet.title)

    # read a line number
    for lineNum in range(1,sheet.max_row + 1):
        lineList = []
        for columnNum in range(1,sheet.max_column + 1) :# take data
            value = sheet.cell(row=lineNum,column=columnNum).value
            ifvalue ! =None:
                lineList.append(value)
        print(lineList)

path = r'C:\Users\asus\Desktop\e.xlsx'
readXlsx(path)
Copy the code

Running results:

 

 

 

Learn together and make progress together. If there are any mistakes, please comment