|
最近手头有大量的载体需要生成载体图谱,而手头的Vector NTI不支持载体图谱导出功能,更不用提批量化操作。
所以研究了下pywinauto模块,期间遇到各种奇葩问题,不得不走弯路去解决。
附上代码,可能对有些同学有帮助。
Python版本:3.6
from pywinauto.application import Application
from pywinauto.keyboard import SendKeys
import pywinauto.mouse
import pywinauto.clipboard
import time
import re
import pymysql
import os
import os.path
import sys
#coding=utf-8
def modifyFile(vname, fname, newname):
f = open(fname, "r")
o = open(newname, "w")
i = 0
vname = re.sub(" ", "_", vname)
vname = re.sub(r"\\", "/", vname)
for ln in f.readlines():
if not i:
#把gb文件的第一行中的Exported替换成载体名称,这样可以显示在载体图谱中。否则载体图谱中的载体名称为Exported
ln = re.sub("^LOCUS Exported", "LOCUS " + vname, ln)
i = i+1
try:
o.write(ln)
except:
o.write(ln.encode("gbk","ignore").decode("gbk"))
o.close()
f.close()
def focus(mWin):
#这个函数是Focus在指定窗口设为焦点时(bring to foreground)用的,模块中有SetFocus()函数,然而有时候没用,换句话说,就是不靠谱。
mWin.Minimize()
time.sleep(0.1)
mWin.Restore()
time.sleep(0.1)
def saveImage(path):
#这个函数是将剪切板中的数据直接粘贴到windows的画图程序中,然后保存为png图片
#本来想用PIL的ImageGrab模块,直接读取剪切板中的图片数据,然后直接保存。
#但是,ImageGrab.grabclipboard()返回的是None;原来是VECTOR NTI的图片格式是wmf的
#还尝试过将wmf文件读取出来,然后保存为文件,再用PIL的Image转换成png,结果报错,说渲染wmf时候出错。
mspaint = Application().start(r"mspaint.exe")
mspaintWin = mspaint.top_window_()
mspaintWin.SetFocus()
SendKeys("^v^s")
# mspaintWin.print_control_identifiers()
# dlg = mspaint.window_(title_re=u".*保存.*", class_name="#32770")
# dlg.Edit.SetEditText(path)
# dlg[u'保存'].click()
mspaint['保存为'].edit1.SetText(path)
# mspaint['保存为'].Save.Click()
SendKeys("{ENTER}")
time.sleep(0.1)
mspaint.kill()
time.sleep(0.1)
def run():
app = Application().connect(path=r"C:\Program Files (x86)\Invitrogen\Vector NTI Advance 11\Vector NTI 10.exe")
mWin = app.window(title_re="Vector NTI.*")
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
vname = row[0]
mapurl = row[1]
fname = row[2]
MapMD5 = row[3]
SeqMD5 = row[4]
picname = MapMD5+".png"
if os.path.exists(r'E:\tmp' + "\\" + SeqMD5+".gb") and os.path.exists(r'E:\tmp' + "\\" + picname):
continue;
else:
try:
os.remove(r'E:\tmp' + "\\" + SeqMD5+".gb")
os.remove(r'E:\tmp' + "\\" + picname)
except:
pass
print(picname)
newfn = "tmp.gb"
newname = r'E:\tmp' + "\\" + newfn
fname = r'E:\wamp\ReBuild\www\uploads' + "\\" + fname
modifyFile(vname, fname, newname)
focus(mWin)
SendKeys('^o')
dlg = app.window(title_re=".*Open.*", class_name="#32770")
dlg.Edit.TypeKeys(newfn)
dlg[u'OK'].click()
SendKeys('{ENTER}')
# mWin['Graphics pane'].print_control_identifiers()
pywinauto.mouse.click(button='left', coords=(422, 121))
SendKeys(']]]')
pywinauto.mouse.right_click(coords=(422, 121))
for down in range(5):
SendKeys('{DOWN}')
time.sleep(0.2)
SendKeys('{ENTER 2}')
time.sleep(0.2)
saveImage(picname)
SendKeys('^s')
dlg = app.window(title_re=".*Save.*", class_name="#32770")
dlg.Edit.SetEditText(SeqMD5+".gb")
dlg[u'OK'].click()
SendKeys('{ENTER}')
focus(mWin)
SendKeys("%fl{ENTER}")
# for i in range(3):
# SendKeys('{DOWN}')
# time.sleep(0.2)
# SendKeys("{ENTER}")
# oringal = sys.stdout
# sys.stdout = open(r'e:\redirect.txt', 'w')
# mWin.print_control_identifiers(depth=2)
# sys.stdout = oringal
db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="xxx", charset='utf8')
cursor = db.cursor()
sql = "这句话我就省略了,涉及到一些敏感字段"
try:
#跑着跑着VECTOR NTI就挂了
run()
except Exception as e:
run()
print("Damn!!")
db.close()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 09:06
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社