python -- Keyboard and mouse simulation, reading and creating word files, reading xlsx files

Catalog

 

Keyboard simulation

Mouse simulation

Read doc file and docx file

Create word file

Read 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)

Operation result:

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)

 

Mouse simulation

Example: it is equivalent to pressing twice continuously on the (30, 40) position of the interface.

import win32api
import win32con
import time

#Set the position of the mouse
win32api.SetCursorPos([30,40])
time.sleep(0.1)
#Press the left mouse button
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0)
#Left mouse button up
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)

Operation result:

 

 

Read doc file and docx file

Example: read doc file

import win32com
import win32com.client

def readWordFile(path):
    #Call the word function of the system to process doc and docxl files.
    mw = win32com.client.Dispatch('Word.Application')
    #Open file
    doc = mw.Documents.Open(path)
    #Take each paragraph out of the document
    for paragraph in doc.Paragraphs:
        line = paragraph.Range.Text
        print(line)
    #Close file
    doc.Close()
    #Quit word
    mw.Quit()

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

Operation result:

 

Create 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 documents visible
    word.Visible = True
    #create documents
    doc = word.Documents.Add()
    #Writing content
    #Write from scratch
    r = doc.Range(0,0)
    r.InsertAfter('Hello'+name + '\n')
    r.InsertAfter('      Then WoW as much...\n')
    #Storage file
    doc.SaveAs(filename)
    #Close file
    doc.Close()
    # Quit word
    word.Quit()

filenames = ['Zhang San','Li Si','Wang Wu']
for name in filenames:
    filename = os.path.join(os.getcwd(),name)
    makeWordFile(filename,name)

Operation result:

 

Read xlsx file

To read xlsx files, you need to install an openpyxl package first

Table file to be read

Example: display the names of all tables

from openpyxl.reader.excel import load_workbook

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

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

Operation result:

Example: reading a table

from openpyxl.reader.excel import load_workbook

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

    # Take out a form
    sheet = file.worksheets[0]
    #Maximum number of rows
    print(sheet.max_row)
    #Maximum column number
    print(sheet.max_column)
    #Table name
    print(sheet.title)

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

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

Operation result:

 

 

 

Study together, progress together -. - if there are mistakes, please comment.

Tags: Windows Excel

Posted on Sat, 02 Nov 2019 08:45:11 -0700 by JustinM01