You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.8 KiB
179 lines
5.8 KiB
import win32gui, win32con, time |
|
import win32api |
|
import ctypes |
|
|
|
def find_window(title=None, class_name=None): |
|
# 任一参数为 None 即忽略该条件 |
|
hwnd = win32gui.FindWindow(class_name, title) |
|
return hwnd if hwnd else None |
|
|
|
|
|
def send_text_to_window(hwnd, text): |
|
"""向窗口发送文本""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.2) |
|
|
|
for char in text: |
|
# 获取虚拟键码 |
|
vk_code = win32api.VkKeyScan(char) |
|
|
|
if vk_code != -1: |
|
# 提取低字节(虚拟键码)和高字节(修饰键) |
|
vk = vk_code & 0xFF |
|
shift_state = (vk_code >> 8) & 0xFF |
|
|
|
# 如果需要 Shift |
|
if shift_state & 1: # Shift 键 |
|
win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0) |
|
time.sleep(0.01) |
|
|
|
# 按下键 |
|
win32api.keybd_event(vk, 0, 0, 0) |
|
time.sleep(0.01) |
|
# 释放键 |
|
win32api.keybd_event(vk, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
|
|
# 释放 Shift(如果按下了) |
|
if shift_state & 1: |
|
time.sleep(0.01) |
|
win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
|
|
time.sleep(0.02) # 字符之间的延迟 |
|
|
|
#发送 Alt+D 快捷键(聚焦地址栏) |
|
def send_alt_d(hwnd): |
|
"""发送 Alt+D 快捷键(聚焦地址栏)""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.2) |
|
win32api.keybd_event(win32con.VK_MENU, 0, 0, 0) # Alt |
|
time.sleep(0.05) |
|
win32api.keybd_event(ord('D'), 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.keybd_event(ord('D'), 0, win32con.KEYEVENTF_KEYUP, 0) |
|
win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
time.sleep(0.3) |
|
|
|
|
|
#发送 Ctrl+A 快捷键(全选) |
|
def send_ctrl_a(hwnd): |
|
"""发送 Ctrl+A 快捷键(全选)""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.2) |
|
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.keybd_event(ord('A'), 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.keybd_event(ord('A'), 0, win32con.KEYEVENTF_KEYUP, 0) |
|
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
time.sleep(0.3) |
|
|
|
|
|
#发送 Enter 键 |
|
def send_enter(hwnd): |
|
"""发送 Enter 键""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.1) |
|
win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
time.sleep(0.2) |
|
|
|
# |
|
def send_tab(hwnd, count=1): |
|
"""发送 Tab 键(用于导航焦点)""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.1) |
|
for _ in range(count): |
|
win32api.keybd_event(win32con.VK_TAB, 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.keybd_event(win32con.VK_TAB, 0, win32con.KEYEVENTF_KEYUP, 0) |
|
time.sleep(0.1) |
|
time.sleep(0.2) |
|
|
|
#聚焦文件列表窗口 |
|
def focus_file_list(hwnd): |
|
"""将焦点移到文件列表区域""" |
|
win32gui.SetForegroundWindow(hwnd) |
|
time.sleep(0.2) |
|
|
|
# 使用鼠标点击文件列表区域(最可靠的方法) |
|
try: |
|
# 获取窗口位置和大小 |
|
left, top, right, bottom = win32gui.GetWindowRect(hwnd) |
|
width = right - left |
|
height = bottom - top |
|
|
|
# 文件列表通常在窗口中间偏下的位置 |
|
# 计算文件列表的大概位置(窗口中心偏下,避开地址栏和按钮区域) |
|
click_x = left + width // 2 |
|
click_y = top + height // 2 + 30 # 稍微偏下一点,避开地址栏 |
|
|
|
# 点击文件列表区域 |
|
win32api.SetCursorPos((click_x, click_y)) |
|
time.sleep(0.1) |
|
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) |
|
time.sleep(0.05) |
|
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) |
|
time.sleep(0.2) |
|
except Exception as e: |
|
print(f"点击文件列表失败,使用Tab键方式: {e}") |
|
# 如果点击失败,使用Tab键导航(备用方案) |
|
send_tab(hwnd, 1) |
|
time.sleep(0.2) |
|
|
|
|
|
def modify_file_dialog_path_and_import_all(target_path=""): |
|
if not target_path: |
|
return False |
|
""" |
|
修改文件对话框的路径并导入所有文件 |
|
|
|
参数: |
|
target_path: 目标文件夹路径,默认为 C://work/batchPrint |
|
""" |
|
# 1. 找到"打开"窗口 |
|
print("正在查找'打开'窗口...") |
|
hwnd = find_window(title='打开') |
|
if not hwnd: |
|
print("未找到'打开'窗口,请确保文件对话框已打开") |
|
return False |
|
|
|
print(f"✓ 找到'打开'窗口,句柄: {hwnd}") |
|
time.sleep(0.5) |
|
|
|
# 2. 激活窗口并聚焦地址栏(使用 Alt+D 快捷键) |
|
print("正在聚焦地址栏...") |
|
send_alt_d(hwnd) |
|
time.sleep(0.3) |
|
|
|
# 3. 清空地址栏并输入新路径 |
|
print(f"正在设置路径为: {target_path}") |
|
send_ctrl_a(hwnd) # 全选现有路径 |
|
time.sleep(0.2) |
|
|
|
# 输入新路径 |
|
send_text_to_window(hwnd, target_path) |
|
time.sleep(0.3) |
|
|
|
# 按 Enter 确认路径 |
|
send_enter(hwnd) |
|
print("✓ 路径已设置") |
|
time.sleep(1.5) # 等待路径加载完成 |
|
|
|
# 4. 将焦点移到文件列表 |
|
print("正在将焦点移到文件列表...") |
|
focus_file_list(hwnd) |
|
time.sleep(0.3) |
|
|
|
# 5. 选择所有文件 |
|
print("正在选择所有文件...") |
|
send_ctrl_a(hwnd) # Ctrl+A 选择所有文件 |
|
time.sleep(0.5) |
|
print("✓ 已选择所有文件") |
|
|
|
# 6. 点击"打开"按钮(使用 Enter 键) |
|
print("正在确认打开...") |
|
send_enter(hwnd) |
|
print("✓ 已确认打开") |
|
|
|
return True
|
|
|