上文说到小拍我把自动填写表格的技术准备做好了,接下来编写控制鼠标键盘的程序在逻辑上其实一点也不难。
自己先手动填表走一趟,对控制鼠标键盘先做什么后做什么心里有个数儿。
记住需要鼠标点的位置坐标,为了保持坐标位置不变,有些需要先把窗口最大化。
表格项与项之间切换有用tab键的,有用向下键的。组合键的功能很强大(不知道组合键怎么组合没关系,到网上搜,没用之前我也不知道)。
自动往文本框里输入中文字符费了不少劲(吃了拿来主义不求甚解的亏),详见前面几篇,绕了一个大圈又回到原点。
还有一个很花时间的地方就是控制节奏。代码发出命令很快,可某些命令执行起来受各种因素制约会滞后、跟不上节奏。
但后续代码可不管那个,命令发出即认为是执行完毕,从而继续发号施令......接下来的命令虽然得到执行,却不是在预设的情景下执行,执行越多错乱越多直到变成一团乱麻或干脆陷入到死循环。
框架建立很容易,费时间的是调整细节部分,这时候最能体会那句“细节决定成败”,
不过也许正是因为费的那些劲儿,做完后才这么有成就感。
自己欣赏了半天“电脑自动填表格”还不满足,又把西门吹雪小胖墩...呃,不是,是小少爷...请过来一起看。
虽然在炫的过程中掉了几次链子,被西门吹雪小胖墩鄙视了几下,
但西门吹雪小胖墩还是认识到python的强大,说生日会那天他也要秀給小伙伴们看,
以后再有这种枯燥重复耗时的工作他就直接交给我用 python处理啦
我想跟他说鱼和渔,他说他要赛前准备~拜拜喽~
控制鼠标键盘自动填表格
控制鼠标键盘输出中文以及程序调试
# partyFormFiller.py - Automatically fills in the form (English character).
# 专心解决填写中文字符
import pyautogui, time, csv
import subprocess
# Set these to the correct coordinates for your particular computer.3
schoolField = (439, 486)
submitButton = (474, 544)
submitButtonColor = (49, 123, 253)
submitAnotherLink = (478, 422)
# custermer list from csv file
customers = []
# convert to dictionary variable
# add 2: switch to Chinese version
partyFile = open('party.csv', encoding='utf-8')
partyReader = csv.reader(partyFile)
# read data from csv file
for row in partyReader:
if partyReader.line_num == 1:
continue
food = row[3]
foods = food.split()
customer = {
'school': row[0],
'attend': row[1],
'headcount': row[2],
'foodOption': foods,
'taboos': row[4],
'contact': row[5]
}
customers.append(customer)
partyFile.close()
# slow down pace of pyautogui action
pyautogui.PAUSE = 1
# get form window on focus
pyautogui.click((100, 100))
# full screen
pyautogui.hotkey('ctrl', 'command', 'f')
# add 1, empty clipboard
subprocess.Popen(['pbcopy'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
# automate filling in form
for customer in customers:
# To confirm the form is ready to fill in
time.sleep(1)
# To bottom of the page
pyautogui.hotkey('command', 'down')
# Wait until the form page has loaded.
while not pyautogui.pixelMatchesColor(submitButton[0], submitButton[1], submitButtonColor):
time.sleep(0.5)
# To top of the page for input
pyautogui.hotkey('command', 'up')
schoolList = ['少林', '武当', '昆仑', '峨眉', '五岳', '逍遥', '丐帮', '全真', '古墓', '移花宫']
school = int(customer['school'])
print('Entering {} info...'.format(schoolList[school-1]))
# Click the first field
pyautogui.click(schoolField[0], schoolField[1])
# Fill out the 门派 field.
for order in range(school):
pyautogui.press('down')
pyautogui.press('enter')
pyautogui.press('\t')
# Fill out the 是否参加 field.
if customer['attend'] == '1':
pyautogui.typewrite(['space', '\t'])
# Fill out 多少人 field.
pyautogui.typewrite(customer['headcount'] + '\t', 0.25)
# add 8: give more time to output text
time.sleep(1)
# Fill out 食物 field.
# 1-肉夹馍;2-山东大饼;3-手撕饼;4-煎饼果子 5-馒头;6-肉包子;7-素馅包子
if '1' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '2' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '3' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '4' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '5' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '6' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
if '7' in customer['foodOption']:
pyautogui.typewrite(['space', '\t'])
else:
pyautogui.press('\t')
# Fill out 禁忌 field.
# add 3: write taboos to clipboard
# p1 = subprocess.Popen(['echo', customer['taboos']], stdout=subprocess.PIPE)
# p2 = subprocess.Popen(['pbcopy'], stdin=p1.stdout, stdout=subprocess.PIPE)
#
# pyautogui.press(str(customers.index(customer)))
# pyautogui.press('\b')
import pyperclip
pyperclip.copy(customer['taboos'])
pyautogui.hotkey('command', 'v')
pyautogui.press('\t')
# Fill out 手机号码 field.
pyautogui.typewrite(customer['contact'] + '\t', 0.25)
# Click Submit.
pyautogui.press('enter')
elif customer['attend'] == '2':
pyautogui.typewrite(['down', 'space'])
pyautogui.click(100, 100)
# To bottom of the page
pyautogui.hotkey('command', 'down')
pyautogui.click(submitButton)
elif customer['attend'] == '3':
pyautogui.typewrite(['down', 'down', 'space'])
pyautogui.click(100, 100)
# To bottom of the page
pyautogui.hotkey('command', 'down')
pyautogui.click(submitButton)
# Wait until form page has loaded.
print('Clicked Submit.')
# Click the Submit another response link.
# add 4: give more time to load next page
time.sleep(1)
pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])
# add 5: give more time to load new form
time.sleep(1)
# exit full screen mode
pyautogui.hotkey('ctrl', 'command', 'f')