python pyautogui与openpyxl运用

openpyxl是python处理excel文件的包,而pyautogui可以实现鼠标和键盘的操纵,两者结合,可以将excel中的数据自动输入erp软件。

erp软件将日常工作流程化,一方面利于整体数据管理,另一方面将工作变的非常枯燥。利用pyautogui可以解放双手和双眼。程序运行之后,剩下的只有干等了…

由于边工作边写代码花了两天时间写完。

openpyxl对我来说相对简单(从excel vba入门编程,大约两个小时熟悉,pyautogui花费了不少时间熟悉,总体来说,两个包都不难,组合起来十分实用,至少在不懂编程的人面前展示,可以用来唬人了。

代码有点长,本来想封装的,无奈虽然跟着书写了两个类的封装继承,模模糊糊似乎明白了,其实还是不明白,退求其次用了函数来简化代码。

python中代码的缩进很重要,但是不知道为啥粘到就成这种效果了,很无语。

代码如下:


__author__ = 'Administrator'

#项目设想:将excel数据自动输入erp软件

import pyautogui

import time

import openpyxl

#pmdx=(1920,1080)#屏幕大小设定

date1='2019-03-14'

bjglx=128

bjgly=34

bjdjx=150

bjdjy=64

scrqx=605

scrqy=335

scrq2x=535

scrq2y=337

#班次设定

bancix=700

banciy=337

zaox=681

zaoy=414

zhongx=681

zhongy=430

wanx=681

wany=398

#工序-注塑

gongxux=951

gongxuy=337

zhusux=897

zhusuy=367

#设备坐标

shebeix=1124

shebeiy=336

a1_a8_a15_a22_b6x=1128

a1_a8_a15_a22_b6y=369

a2_a9_a16_a23_b7x=1124

a2_a9_a16_a23_b7y=383

a3_a9_a17_b1_b8x=1124

a3_a9_a17_b1_b8y=397

a4_a10_a18_b2x=1124

a4_a10_a18_b2y=411

a5_a11_a19_b3x=1124

a5_a11_a19_b3y=425

a6_a12_a20_b4x=1124

a6_a12_a20_b4y=439

a7_a13_a21_b5x=1124

a7_a13_a21_b5y=453

#班组坐标

bzax=1400

bzay=440

bzbx=1400

bzby=460

bzcx=1372

bzcy=364#双击班组B一次再单击

#点击产品

chanpinx=665

chanpiny=295

cpbmx=660

cpbmy=357

tccpx=554

tccpy=275

shurux=638#输入数据

shuruy=321

chaxunx=586

chaxuny=278

xuanxiangx=556

xuanxiangy=400

#人工工时

gongshix=1291

gongshiy=319#输入数据

#排产编号

paichanx=652

paichany=651

pcxxx=566

pcxxy=435

#退出排产编号

pctcx=562

pctcy=302

#输入数量

rukushux=650

rukushuy=416

#保存

baocunx=517

baocuny=235

#退出报缴

tcbjx=485

tcbjy=235

#完全退出

wqtcx=485

wqtcy=297

#读取Excel表格数据

wb=openpyxl.load_workbook(r'C:\Documents and Settings\Administrator\PycharmProjects\项目开发\操纵excel\报缴用表.xlsx')

sheet=wb.get_sheet_by_name('报缴用表')

#防错设定

pyautogui.PAUSE=1

pyautogui.FAILSAFE=True

time.sleep(5)

#定义一个机床调用函数坐标调用

def jichuang(jcx,jcy):#机床A1234567

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx,jcy,duration=0.25)

pyautogui.click()

def jichuang2(jcx,jcy,jcx1,jcy1):#机床A891011121314

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx,jcy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx1,jcy1,duration=0.25)

pyautogui.click()

def jichuang3(jcx,jcy,jcx1,jcy1,jcx2,jcy2):#机床15.16.17.18.19.20.21

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx,jcy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx1,jcy1,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx2,jcy2,duration=0.25)

pyautogui.click()

def jichuang4(jcx,jcy,jcx1,jcy1,jcx2,jcy2,jcx3,jcy3):#机床22.23.1.2.3.4.5B

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx,jcy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx1,jcy1,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx2,jcy2,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx3,jcy3,duration=0.25)

pyautogui.click()

def jichuang5(jcx,jcy,jcx1,jcy1,jcx2,jcy2,jcx3,jcy3,jcx4,jcy4):#机床678

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx,jcy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx1,jcy1,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx2,jcy2,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx3,jcy3,duration=0.25)

pyautogui.click()

pyautogui.moveTo(shebeix,shebeiy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(jcx4,jcy4,duration=0.25)

pyautogui.click()

#循环读数

for i in range(2,sheet.max_row+1):

pyautogui.moveTo(bjglx,bjgly,duration=0.25)

pyautogui.click()

pyautogui.moveTo(bjdjx,bjdjy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(scrqx,scrqy,duration=0.25)

pyautogui.dragTo(scrq2x,scrq2y,duration=0.25)

pyautogui.typewrite(['delete'])

pyautogui.typewrite(date1)

#工序选择注塑

pyautogui.moveTo(gongxux,gongxuy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(zhusux,zhusuy,duration=0.25)

pyautogui.click()

#选择班次这里有分支A组,B组,C组有区别

#A组

if sheet.cell(i,2).value=="A":

pyautogui.moveTo(1429,335,duration=0.25)

pyautogui.click()

pyautogui.moveTo(bzax,bzay,duration=0.25)

pyautogui.click()

#B组

if sheet.cell(i,2).value=="B":

pyautogui.moveTo(1429,335,duration=0.25)

pyautogui.click()

pyautogui.moveTo(bzbx,bzby,duration=0.25)

pyautogui.click()

#C组

if sheet.cell(i,2).value=="C":

pyautogui.moveTo(1429,335,duration=0.25)

pyautogui.click()

pyautogui.moveTo(bzbx,bzby,duration=0.25)

pyautogui.click()

pyautogui.moveTo(1429,335,duration=0.25)

pyautogui.click()

pyautogui.moveTo(bzcx,bzcy,duration=0.25)

pyautogui.click()

#早中晚班选择

if sheet.cell(i,1).value=="早":

pyautogui.moveTo(bancix,banciy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(zaox,zaoy,duration=0.25)

pyautogui.click()

if sheet.cell(i,1).value=="中":

pyautogui.moveTo(bancix,banciy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(zhongx,zhongy,duration=0.25)

pyautogui.click()

if sheet.cell(i,1).value=="晚":

pyautogui.moveTo(bancix,banciy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(wanx,wany,duration=0.25)

pyautogui.click()

#机床选择

if sheet.cell(i,3).value=="A01":

jichuang(a1_a8_a15_a22_b6x,a1_a8_a15_a22_b6y)

if sheet.cell(i,3).value=="A02":

jichuang(a2_a9_a16_a23_b7x,a2_a9_a16_a23_b7y)

if sheet.cell(i,3).value=="A03":

jichuang(a3_a9_a17_b1_b8x,a3_a9_a17_b1_b8y)

if sheet.cell(i,3).value=="A04":

jichuang(a4_a10_a18_b2x,a4_a10_a18_b2y)

if sheet.cell(i,3).value=="A05":

jichuang(a5_a11_a19_b3x,a5_a11_a19_b3y)

if sheet.cell(i,3).value=="A06":

jichuang(a6_a12_a20_b4x,a6_a12_a20_b4y)

if sheet.cell(i,3).value=="A07":

jichuang(a7_a13_a21_b5x,a7_a13_a21_b5y)

if sheet.cell(i,3).value=="A08":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a1_a8_a15_a22_b6x,a1_a8_a15_a22_b6y)

if sheet.cell(i,3).value=="A09":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a2_a9_a16_a23_b7x,a2_a9_a16_a23_b7y)

if sheet.cell(i,3).value=="A10":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a4_a10_a18_b2x,a4_a10_a18_b2y)

if sheet.cell(i,3).value=="A11":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a5_a11_a19_b3x,a5_a11_a19_b3y)

if sheet.cell(i,3).value=="A12":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a6_a12_a20_b4x,a6_a12_a20_b4y)

if sheet.cell(i,3).value=="A13":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a6_a12_a20_b4x,a6_a12_a20_b4y)

if sheet.cell(i,3).value=="A14":

jichuang2(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y)

if sheet.cell(i,3).value=="A15":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a1_a8_a15_a22_b6x,a1_a8_a15_a22_b6y)

if sheet.cell(i,3).value=="A16":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a2_a9_a16_a23_b7x,a2_a9_a16_a23_b7y)

if sheet.cell(i,3).value=="A17":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a3_a9_a17_b1_b8x,a3_a9_a17_b1_b8y)

if sheet.cell(i,3).value=="A18":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a4_a10_a18_b2x,a4_a10_a18_b2y)

if sheet.cell(i,3).value=="A19":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a5_a11_a19_b3x,a5_a11_a19_b3y)

if sheet.cell(i,3).value=="A20":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a6_a12_a20_b4x,a6_a12_a20_b4y)

if sheet.cell(i,3).value=="A21":

jichuang3(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y)

if sheet.cell(i,3).value=="A22":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a1_a8_a15_a22_b6x,a1_a8_a15_a22_b6y)

if sheet.cell(i,3).value=="A23":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a2_a9_a16_a23_b7x,a2_a9_a16_a23_b7y)

if sheet.cell(i,3).value=="B01":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a3_a9_a17_b1_b8x,a3_a9_a17_b1_b8y)

if sheet.cell(i,3).value=="B02":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a4_a10_a18_b2x,a4_a10_a18_b2y)

if sheet.cell(i,3).value=="B03":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a5_a11_a19_b3x,a5_a11_a19_b3y)

if sheet.cell(i,3).value=="B04":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a6_a12_a20_b4x,a6_a12_a20_b4y)

if sheet.cell(i,3).value=="B05":

jichuang4(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y)

if sheet.cell(i,3).value=="B06":

jichuang5(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a1_a8_a15_a22_b6x,a1_a8_a15_a22_b6y)

if sheet.cell(i,3).value=="B07":

jichuang5(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a2_a9_a16_a23_b7x,a2_a9_a16_a23_b7y)

if sheet.cell(i,3).value=="B08":

jichuang5(a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a7_a13_a21_b5x,a7_a13_a21_b5y,a3_a9_a17_b1_b8x,a3_a9_a17_b1_b8y)

#点击产品进入报缴页面

pyautogui.moveTo(chanpinx,chanpiny,duration=0.25)

pyautogui.click()

#单击产品编码,单击退出,单击产品编码取消上次遗留信息

pyautogui.moveTo(cpbmx,cpbmy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(tccpx,tccpy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(cpbmx,cpbmy,duration=0.25)

pyautogui.click()

#单击产品输入窗口输入数据,单击查询,选项打钩,退出

pyautogui.moveTo(shurux,shuruy,duration=0.25)

pyautogui.click()

pyautogui.typewrite(sheet.cell(i,4).value)

pyautogui.moveTo(chaxunx,chaxuny,duration=0.25)

pyautogui.click()

pyautogui.typewrite(['enter'])#防止没有产品编码时弹出页面

pyautogui.moveTo(xuanxiangx,xuanxiangy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(tccpx,tccpy,duration=0.25)

pyautogui.click()

#单击排产编号,排产计划选项打钩,退出

pyautogui.moveTo(paichanx,paichany,duration=0.25)

pyautogui.click()

pyautogui.moveTo(pcxxx,pcxxy,duration=0.25)

pyautogui.click()

pyautogui.moveTo(pctcx,pctcy,duration=0.25)

pyautogui.click()

#合格数量输入,人工工时输入

pyautogui.moveTo(rukushux,rukushuy,duration=0.25)

pyautogui.doubleClick()

pyautogui.typewrite(['delete'])

pyautogui.typewrite(str(sheet.cell(i,7).value))

pyautogui.moveTo(gongshix,gongshiy,duration=0.25)

pyautogui.click()

pyautogui.typewrite('1')

#保存数据,退出报缴界面

pyautogui.moveTo(baocunx,baocuny,duration=0.25)

pyautogui.click()

pyautogui.typewrite(['enter'])

pyautogui.moveTo(tcbjx,tcbjy,duration=0.25)

pyautogui.click()

#完全退出

pyautogui.moveTo(wqtcx,wqtcy,duration=0.25)

pyautogui.click()

time.sleep(10)#留出时间调试

你可能感兴趣的:(python pyautogui与openpyxl运用)