python 开发金山打字通辅助脚本

python 开发金山打字通辅助脚本python开发金山打字通辅助脚本_自动打字脚本

大家好,欢迎来到IT知识分享网。

python 开发金山打字通辅助脚本

  • 环境
  • 开发步骤
  • 代码

环境

这个脚本主要是靠python的pyautogui库实现,使用五笔输入法进行输入。
请先下载安装搜狗五笔或者别的五笔输入法,搜狗输入法下载

from xml.dom.minidom import parse
from lxml import etree
import requests, pyautogui, json, os, time, string

开发步骤

通过研究金山打字通的文件,发现金山打字通的文章存储在xml文件内
在这里插入图片描述
文件内容:
在这里插入图片描述

1.搜索出金山打字通根目录下所有的xml文件

def get_files(path,rule):
    all = []
		# fpathe 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # fs 表示该文件夹下的文件list
    for fpathe, dirs, fs in os.walk(path):  # os.walk获取所有的目录
        
        for f in fs:
            if f.endswith(rule):  # 判断是否是".xml"结尾
                all.append(str(fpathe).replace('\\', '/', 100) + r'/' + str(f))
    return all

2.解析xml文件

py_text = []
file = r"D:\Program Files (x86)\Typeeasy\11.5\course\test\cn\1.xml"  #xml 文件路径
DOMTree=parse(file)		# 打开xml文件
booklist=DOMTree.documentElement  #获取XML文档对象
courseInfo = booklist.getElementsByTagName('courseInfo')  #查找courseInfo元素
if courseInfo:
    content = booklist.getElementsByTagName('item')	#查找item元素
    for con in content:
        py_text.append(con.getAttribute('comparision')) #获取元素item的comparision属性内容,加入到列表

3.获取五笔字根
利用爬虫获取五笔字根
网站:https://qq.ip138.com/wb/wb.asp
在这里插入图片描述

def trf_get(text):
    url = 'https://qq.ip138.com/wb/wb.asp'
    data = { 
   'querykey': text}
    headers = { 
   
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62'
    }
    res = requests.post(url, data=data, headers=headers)	#使用post请求获取数据
    html = etree.HTML(res.text)	#解析html数据
    bian = html.xpath('/html/body/div/div[2]/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[2]/p/text()')	#定位到数据,并获取
    
    if bian:
        return bian[0]
    else:
        return text

4.将获取到的字根进行保存与字根查询

def trf_jian(text):
    file = open('trf_text.json', 'r', encoding='utf-8')	# 创建存储字根的文件
    zk = json.loads(file.read())
    if text in zk:	# 判断本地是否存在该文字
        return zk[text]
    else: # 如果不存在上网查询
        res = trf_get(text)	 # 调用查询函数
        j = { 
   text: res}
        zk.update(j)	#将数据保存到本地
        with open('trf_text.json', 'w', encoding='utf-8') as f:
            f.writelines(json.dumps(zk))
            f.close()	# 清理内存
    file.close() # 清理内存
    return res

5.输入功能

pun = string.punctuation	# 所有的标点字符
file_name = '荷塘月色'	# 文章标题
text = sa(file_name)	# 调用查询函数
for t in text:
    time.sleep(0.8)	# 速度限制
    a = trf_jian(t)	# 查询文字
    print(t, a)
    pyautogui.typewrite(a)	# 输入字根
    if a not in pun:
        pyautogui.press('space')

代码

import requests, pyautogui, json, os, time, string
from xml.dom.minidom import parse
from lxml import etree


def trf_get(text):
    url = 'https://qq.ip138.com/wb/wb.asp'
    data = { 
   'querykey': text}
    headers = { 
   
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62'
    }
    res = requests.post(url, data=data, headers=headers)
    html = etree.HTML(res.text)
    bian = html.xpath('/html/body/div/div[2]/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[2]/p/text()')
    if bian:
        return bian[0]
    else:
        return text


def trf_jian(text):
    file = open('trf_text.json', 'r', encoding='utf-8')
    zk = json.loads(file.read())
    if text in zk:
        return zk[text]
    else:
        res = trf_get(text)
        j = { 
   text: res}
        zk.update(j)
        with open('trf_text.json', 'w', encoding='utf-8') as f:
            f.writelines(json.dumps(zk))
            f.close()
    file.close()
    return res


def get_files(path, rule):
    all = []
    # fpathe 表示当前正在访问的文件夹路径
    # dirs 表示该文件夹下的子目录名list
    # fs 表示该文件夹下的文件list
    for fpathe, dirs, fs in os.walk(path):  # os.walk获取所有的目录
        for f in fs:
            if f.endswith(rule):  # 判断是否是".sfx"结尾
                all.append(str(fpathe).replace('\\', '/', 100) + r'/' + str(f))
    return all


def sa(file_name):
    py_text = []
    b = get_files(path=r'D:\Program Files (x86)\Typeeasy', rule=".xml")
    for i in b:
        DOMTree = parse(i)
        booklist = DOMTree.documentElement
        courseInfo = booklist.getElementsByTagName('courseInfo')

        if courseInfo:
            sa_file_name = courseInfo[0].getAttribute('alias')
            if sa_file_name == file_name:
                content = booklist.getElementsByTagName('item')
                for con in content:
                    py_text.append(con.getAttribute('comparision'))
                return ''.join(py_text)
    return ''


if __name__ == "__main__":
    time.sleep(5)
    pun = string.punctuation
    file_name = '荷塘月色'
    text = sa(file_name)
    for t in text:
        time.sleep(0.8)
        a = trf_jian(t)
        print(t, a)
        pyautogui.typewrite(a)
        # time.sleep(0.51)
        # pyautogui.screenshot('back.png')
        # if opencv_img.imgs():
        te = [';']
        if not t in te:
            if a not in pun:
                pyautogui.press('space')

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/27499.html

(0)
上一篇 2023-09-18 15:00
下一篇 2023-09-18 19:45

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信