鱼C论坛

 找回密码
 立即注册
查看: 2521|回复: 15

[已解决]如何利用python将中英文混合的文本输入到输入框处

[复制链接]
发表于 2023-5-11 11:09:43 | 显示全部楼层 |阅读模式
50鱼币
以下是可以输出英文到输入框的代码,但是处理不了中文:
  1. # !/usr/bin/env python3
  2. # _*_ coding:utf-8 _*_
  3. """
  4. @File     : AutoInput.py
  5. @Project  : AutoInput
  6. @Time     : 2023/5/10 22:15
  7. @Author   : Gag
  8. @Software : PyCharm
  9. @License  : (C)Copyright 2018-2028, Taogroup-NLPR-CASIA
  10. @Last Modify Time      @Version     @Desciption
  11. --------------------       --------        -----------
  12. 2023/5/10 22:15        1.0             None
  13. """
  14. # 监听键盘以达到快捷键启动程序
  15. import pythoncom
  16. import PyHook3
  17. # 用户选择文件
  18. import tkinter as tk
  19. from tkinter import filedialog
  20. # 用于判断文件的编码格式
  21. import chardet
  22. # pip install chardet -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
  23. # 用于模拟键盘输入
  24. from pymouse import PyMouse
  25. from pykeyboard import PyKeyboard
  26. # pip install PyUserInput -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
  27. import pypinyin
  28. # pip install pypinyin -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
  29. import time


  30. # 判断当前输入的文字是否是中文
  31. def is_chinese(word):
  32.     for ch in word:
  33.         if '\u4e00' <= ch <= '\u9fff':
  34.             return True
  35.     return False


  36. # 判断文件编码格式
  37. def get_encoding(file):
  38.     with open(file, 'rb') as f:
  39.         tmp = chardet.detect(f.read())
  40.         return tmp['encoding']


  41. # 监听键盘
  42. def onKeyboardEvent(event):
  43.     '''
  44.     # 监听键盘事件
  45.     print("MessageName:", event.MessageName)
  46.     print("Message:", event.Message)
  47.     print("Time:", event.Time)
  48.     print("Window:", event.Window)
  49.     print("WindowName:", event.WindowName)
  50.     print("Ascii:", event.Ascii, chr(event.Ascii))
  51.     print("Key:", event.Key)
  52.     print("KeyID:", event.KeyID)
  53.     print("ScanCode:", event.ScanCode)
  54.     print("Extended:", event.Extended)
  55.     print("Injected:", event.Injected)
  56.     print("Alt", event.Alt)
  57.     print("Transition", event.Transition)
  58.     print("---")
  59.     '''

  60.     # 通过 键盘上的 Insert 按键 触发自动输入
  61.     if event.Key == "Insert":
  62.         # 打开文件
  63.         # 创建对象
  64.         root = tk.Tk()
  65.         root.withdraw()
  66.         # 获取文件路径
  67.         FilePath = filedialog.askopenfilename()
  68.         print(FilePath)
  69.         # 获取文件编码格式
  70.         Encoding = get_encoding(FilePath)
  71.         print("%s的文件编码格式是%s:" % (FilePath, Encoding))
  72.         # 创建文件对象
  73.         f = open(FilePath, 'r', encoding=Encoding)
  74.         lines = f.read()
  75.         # print(lines)

  76.         # 将文件内容输出到指定地点
  77.         # 创建鼠标键盘对象
  78.         m = PyMouse()  # 建立鼠标对象
  79.         k = PyKeyboard()  # 建立键盘对象
  80.         # 获取文字输入位置

  81.         location = m.position()
  82.         m.click(location[0], location[1])
  83.         # 自动输入部分

  84.         k.type_string(lines)

  85.         #关闭文件
  86.         f.close()

  87.     # 不要修改此返回值,否则将对按键进行拦截
  88.     return True


  89. def main():
  90.     # 创建一个“钩子”管理对象
  91.     hm = PyHook3.HookManager()
  92.     # 监听所有键盘事件
  93.     hm.KeyDown = onKeyboardEvent

  94.     # 设置键盘“钩子”
  95.     hm.HookKeyboard()

  96.     # 进入循环,如不手动关闭,程序将一直处于监听状态
  97.     pythoncom.PumpMessages()


  98. if __name__ == "__main__":
  99.     main()
复制代码


我该如何修改代码才能处理中文字符?

最佳答案
2023-5-11 11:09:44
我没看明白,你要把中英文输入到输入框吗,代替键盘输入吗?直接用keyboard库啊,中英文都行。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-5-12 12:57:52 | 显示全部楼层
sfqxx 发表于 2023-5-11 19:13
可以在代码中加上以下两行注释以支持中文输入:

不是让python输出中文
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-28 04:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表