导出onenote for windows10中的笔记
2025-05-28
基于python的pywinauto库
Written by: Jack-S-H
import datetime
from turtle import title
import pyperclip
import win32gui
from pywinauto import Application
from pywinauto import mouse
from pywinauto import keyboard
import get_excel_rows_as_dict
def get_window_handle(window_title):
"""
获取窗口句柄。
参数:
window_title (str): 窗口标题。
返回:
int: 窗口句柄。如果未找到窗口,返回 None。
"""
hwnd = win32gui.FindWindow(None, window_title)
if hwnd == 0:
print(f"未找到窗口:{window_title}")
return None
return hwnd
def connect_to_window(hwnd):
"""
连接到指定窗口。
参数:
hwnd (int): 窗口句柄。
返回:
pywinauto.application.WindowSpecification: 窗口对象。
"""
try:
app = Application(backend='uia').connect(handle=hwnd)
window = app.window(handle=hwnd)
return window
except Exception as e:
print(f"连接窗口失败:{e}")
return None
def select_text(window, target_title, static_control):
"""
获取选中文本。
参数:
window: 窗口对象。
target_title (str): 目标控件的标题。
static_control (str): 静态控件的名称。
返回:
str: 选中的文本。如果获取失败,返回 None。
"""
try:
# 定位目标控件
target = window.child_window(title=target_title, control_type="ListItem")
if not target.exists():
print(f"未找到目标控件:{target_title}")
return None
# 定位静态控件
static = target.child_window(best_match=static_control)
if not static.exists():
print(f"未找到静态控件:{static_control}")
return None
# 获取控件位置并点击
static.click_input()
# rect = static.rectangle()
# x, y = rect.left + 40, rect.bottom - 10
# mouse.click(coords=(x, y))
# mouse.click(coords=(x, y))
# # 复制文本
# keyboard.send_keys('^c')
# selected_text = pyperclip.paste()
#
# # 如果剪贴板为空,尝试发送回车键
# if not selected_text:
# keyboard.send_keys('~')
# selected_text = pyperclip.paste()
#
# return selected_text
except Exception as e:
print(f"获取选中文本失败:{e}")
return None
def main():
# 窗口标题
window_title = "资产管理"
# 获取窗口句柄
hwnd = get_window_handle(window_title)
if not hwnd:
return
# 连接到窗口
window = connect_to_window(hwnd)
if not window:
return
# 获取excel内容
data_dict = get_excel_rows_as_dict.get_rows_as_dict()
if not data_dict:
raise ValueError("错误:未获取到excel数据,程序终止运行。")
# 自动获取子控件列表
list_items = window.descendants(control_type="ListItem")
# print(list_items)
# 选中单元格,准备输入
target_title = "198" # 目标控件的标题
static_control = "Static5" # 静态控件的名称
select_text(window, target_title, static_control)
fin = list()
for item in list_items[197:]:
fin.append(item.children()[1].window_text())
# 根据控件数量,录入数据
for i in fin:
# 比对数据
# print("获取的字典数据:")
# if i not in data_dict:
# keyboard.send_keys("{DOWN}") # 如果 i 不是 data_dict 的 key,则跳过此次循环
# continue
# keyboard.type_keys(str(data_dict[i][2]))
# keyboard.send_keys('~')
# if (data_dict[i]):
print(i)
print(data_dict[i])
for o in data_dict[i]:
# print(o)
# 获取选中文本
pyperclip.copy("")
keyboard.send_keys('^c')
selected_text = pyperclip.paste()
# print(selected_text)
if selected_text == o:
keyboard.send_keys('~')
elif get_excel_rows_as_dict.pd.isna(o):
# 输入value值
keyboard.send_keys('~')
elif isinstance(o, datetime.datetime):
temp = str(o).split(" ")[0]
# print(temp)
keyboard.send_keys(temp)
keyboard.send_keys('~')
# print(str(o))
# elif get_excel_rows_as_dict.pd.isna(selected_text):
elif selected_text == "":
# print(str(o))
keyboard.send_keys(str(o))
keyboard.send_keys('~')
else:
keyboard.send_keys('~')
if __name__ == "__main__":
main()