当前位置:   article > 正文

python 通过直接操控窗口实现开票软件的快速辅助输入工具。_python findwindowex

python findwindowex

背景: 深增值税开票软件在填入发票的明细时(2016年),没有从exce导入的功能,只能手工一个一个输入,对于明细清单数量多的情况,给财务人员带来过多的工作量。所以考虑做一个辅助输入工具。减轻财务的工作量,降低手工输入的出错率。

思路如下: 发票清单 Excel 格式如下, 因为开票的清单来自于销售的出货清单,都是excel格式(从sap导出的),处理起来很方便,所以也延续用Excel格式。

软件功能需要,实现将上面的清单,自动填入到,深增开软件的 清单填开 表格内。 实现原理也很简单,用程序来模拟手工输入过程,控制光标移动。 同一客户,可以连续打印发票。

选好 excel文件,然后层入。看以下效果图:  

完整代码如下。用了pywin32 来操作窗口,完成光标移动,数据输入。

  1. #!/usr/bin/python
  2. # -*- coding: gbk -*-
  3. # KaiPiaoHaoPengYou.py
  4. #
  5. # Copyright (C) 2016 - xulong <fangkailove@yeah.net>
  6. #
  7. import sys
  8. from ctypes import *
  9. import time
  10. import win32con
  11. import win32api
  12. import win32ui
  13. import wx
  14. import xlrd
  15. import win32gui
  16. import sys
  17. wildcard = "Excel (*.xls)|*.xls"
  18. class MainFrame(wx.Frame):
  19. def __init__(self,*agrs,**kw):
  20. wx.Frame.__init__(self,size=(480,200),*agrs,**kw)
  21. l1=wx.StaticText(self,-1,u"提示:请先启动开票软件,并确定打开了'清单填开' 窗口!!!",(30,100))
  22. l1.SetForegroundColour("Red")
  23. self.t1=wx.TextCtrl(self,-1,"",(30,50),size=(200,-1))
  24. b1=wx.Button(self,-1,u"选择清单文件(EXCEL)",(230,50))
  25. self.Bind(wx.EVT_BUTTON,self.b1Click,b1)
  26. b2=wx.Button(self,-1,u"开始填写",(370,50))
  27. self.Bind(wx.EVT_BUTTON,self.b2Click,b2)
  28. def b1Click(self,evt):
  29. text = self.t1.GetValue()
  30. dlg = wx.FileDialog(self,message=u"选择清单Excel文件",
  31. defaultFile=text , wildcard=wildcard,style=wx.FD_OPEN |wx.FD_CHANGE_DIR)
  32. if dlg.ShowModal() == wx.ID_OK:
  33. text = dlg.GetPath()
  34. self.t1.SetValue(text)
  35. dlg.Destroy()
  36. def b2Click(self,evt):
  37. self.parsexsl(self.t1.GetValue())
  38. def parsexsl(self,filename):
  39. book = None
  40. try:
  41. book=xlrd.open_workbook(filename)
  42. except IOError as e:
  43. pass;
  44. #print ("数据提示:")
  45. if book == None :
  46. wx.MessageBox(message=u"Excel文件不能正常读,确认格式,文件名有误!!",caption=u"提示",parent=self)
  47. return
  48. sheet1 = book.sheet_by_index(0)
  49. #清单行数
  50. nrows = sheet1.nrows
  51. #清单列数
  52. ncols = sheet1.ncols
  53. for rownum in range(1,nrows):
  54. #print ('row',rownum )
  55. #print ("行:",rownum + 1," ",rowline,"\r")
  56. if "%s"%sheet1.cell(rownum,0).value == '---':
  57. message = u"遇到换票标识行(---),需要换票,请检查当前发票无误后,重新填写新发票回到'清单填开'界面,点[确定]后继续\r"
  58. dlg = wx.MessageDialog(self, message, u"提2示", wx.OK|wx.STAY_ON_TOP)
  59. dlg.ShowModal()
  60. continue
  61. f=None
  62. try:
  63. f=win32ui.FindWindow(None,"清单填开")
  64. except win32ui.error as e:
  65. wx.MessageBox(message=u"提示:请先启动开票软件,并确定打开了'清单填开' 窗口",caption=u"提示",parent=self)
  66. return
  67. #取开票软件的窗口类名,此开票软件的类名后辍部分会随机的变化。故需动态取后辍,后续用其拼接其它子窗口的类名。
  68. cln=win32gui.GetClassName(f.GetSafeHwnd())
  69. prex=cln[29:]
  70. #print (prex)
  71. #get main window handle.
  72. win=win32ui.FindWindow("WindowsForms10.Window.8.app.0"+prex,u"清单填开")
  73. #get second level window handle
  74. win2=win32ui.FindWindowEx(win,None,"WindowsForms10.Window.8.app.0"+prex,u"清单填开")
  75. #get grid's parent window
  76. win3=win32ui.FindWindowEx(win2,None,"WindowsForms10.Window.8.app.0"+prex,"")
  77. #get grid's handle
  78. grid=win32ui.FindWindowEx(win3,None,"WindowsForms10.Window.8.app.0"+prex,"")
  79. print 'grid:%x,win3:%x'%(grid.GetSafeHwnd(),win3.GetSafeHwnd())
  80. #fill one line of grid
  81. #move the cursor back to firt cell ,using allow left key
  82. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  83. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  84. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  85. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  86. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  87. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  88. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  89. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  90. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  91. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  92. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  93. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  94. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  95. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  96. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_LEFT, 0)
  97. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_LEFT, 0)
  98. #first column
  99. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_F2, 0)
  100. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_F2, 0)
  101. cp1=win32ui.FindWindowEx(grid,None,"WindowsForms10.Window.8.app.0"+prex,"")
  102. tb=win32ui.FindWindowEx(cp1,None,"WindowsForms10.EDIT.app.0"+prex,None)
  103. print 'tb:%x,cpl:%x'%(tb.GetSafeHwnd(),cp1.GetSafeHwnd())
  104. tb.SendMessage(win32con.WM_SETTEXT,sheet1.cell(rownum,0).value.encode('gbk','ignore'))
  105. #tb.SendMessage(win32con.WM_SETTEXT,'测试1')
  106. #print (sheet1.cell(rownum,0).value)
  107. #second
  108. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  109. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  110. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_F2, 0)
  111. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_F2, 0)
  112. print 'grid:%x,cpl:%x'%(grid.GetSafeHwnd(),cp1.GetSafeHwnd())
  113. cp2=win32ui.FindWindowEx(grid,cp1,"WindowsForms10.Window.8.app.0"+prex,"")
  114. print '2'
  115. tb2=win32ui.FindWindowEx(cp2,None,"WindowsForms10.EDIT.app.0"+prex,None)
  116. #tb2.SendMessage(win32con.WM_SETTEXT,'测试2')
  117. value = sheet1.cell(rownum,1).value.encode('gbk','ignore')
  118. if value.endswith(".0"):
  119. value = value[:-2]
  120. if value.endswith(".00"):
  121. value = value[:-3]
  122. tb2.SendMessage(win32con.WM_SETTEXT,value)
  123. #print (sheet1.cell(rownum,1).value)
  124. #3th
  125. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  126. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  127. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_F2, 0)
  128. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_F2, 0)
  129. cp2=win32ui.FindWindowEx(grid,cp1,"WindowsForms10.Window.8.app.0"+prex,"")
  130. tb2=win32ui.FindWindowEx(cp2,None,"WindowsForms10.EDIT.app.0"+prex,None)
  131. #tb2.SendMessage(win32con.WM_SETTEXT,'测试3')
  132. tb2.SendMessage(win32con.WM_SETTEXT,sheet1.cell(rownum,2).value.encode('gbk','ignore'))
  133. #print (sheet1.cell(rownum,2).value)
  134. #4th
  135. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  136. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  137. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_F2, 0)
  138. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_F2, 0)
  139. cp2=win32ui.FindWindowEx(grid,cp1,"WindowsForms10.Window.8.app.0"+prex,"")
  140. tb2=win32ui.FindWindowEx(cp2,None,"WindowsForms10.EDIT.app.0"+prex,None)
  141. #tb2.SendMessage(win32con.WM_SETTEXT,'4')
  142. value = str(sheet1.cell(rownum,3).value).encode('gbk','ignore')
  143. if value.endswith(".0"):
  144. value = value[:-2]
  145. if value.endswith(".00"):
  146. value = value[:-3]
  147. tb2.SendMessage(win32con.WM_SETTEXT,value)
  148. #print (sheet1.cell(rownum,3).value)
  149. #5th
  150. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  151. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  152. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_F2, 0)
  153. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_F2, 0)
  154. cp2=win32ui.FindWindowEx(grid,cp1,"WindowsForms10.Window.8.app.0"+prex,"")
  155. tb2=win32ui.FindWindowEx(cp2,None,"WindowsForms10.EDIT.app.0"+prex,None)
  156. #tb2.SendMessage(win32con.WM_SETTEXT,'5')
  157. tb2.SendMessage(win32con.WM_SETTEXT,str(sheet1.cell(rownum,4).value).encode('gbk','ignore'))
  158. #print (sheet1.cell(rownum,4).value)
  159. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  160. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  161. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  162. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  163. grid.SendMessage(win32con.WM_KEYDOWN, win32con.VK_TAB, 0)
  164. grid.SendMessage(win32con.WM_KEYUP, win32con.VK_TAB, 0)
  165. #fill one line of grid
  166. #move the cursor to next line . using RETURN key
  167. if rownum < ( nrows -1 ):
  168. win32api.Sleep(3)
  169. grid.PostMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
  170. grid.PostMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)
  171. win32api.Sleep(3)
  172. class MyApp(wx.App):
  173. pass
  174. if __name__ == '__main__':
  175. app=MyApp()
  176. frame=MainFrame(None,title=u"开票助手")
  177. frame.Show(True)
  178. app.MainLoop()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/203191
推荐阅读
相关标签
  

闽ICP备14008679号