You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PyAutoGUI在Outlook邮件段落中设置指定行加粗格式?

解决Outlook中用PyAutoGUI设置文本加粗的问题

问题根源

直接用pyautogui.hotkey()搭配typewrite()的方式无效,核心原因是Outlook的邮件编辑区属于富文本控件——你需要先选中目标文本再触发加粗快捷键,而非先按快捷键再输入内容。

可行解决方案

方案1:分步输入+实时切换加粗状态

拆分文本内容,在需要加粗的段落前后触发快捷键,确保输入时的格式生效:

import pyautogui
import time

# 等待手动切换到Outlook邮件编辑窗口,或用代码激活窗口
time.sleep(2)

# 输入开头非加粗内容
pyautogui.typewrite("Dear [Recipient],\n\nI hope this email finds you well. I wanted to let you know that we have some exciting news to share. We recently launched a new product that has received a lot of positive feedback from our customers. ")

# 触发加粗快捷键(Outlook通用Ctrl+B)
pyautogui.hotkey('ctrl', 'b')
# 输入加粗内容
pyautogui.typewrite("The product is designed to help people [feature 1] and [feature 2]")
# 关闭加粗状态
pyautogui.hotkey('ctrl', 'b')

# 继续输入后续非加粗内容
pyautogui.typewrite(". We believe it has the potential to make a real difference in people's lives.\n\n")

# 再次开启加粗,输入第二段加粗文本
pyautogui.hotkey('ctrl', 'b')
pyautogui.typewrite("Please let me know if you have any questions or would like to learn more.")
pyautogui.hotkey('ctrl', 'b')

# 输入结尾内容
pyautogui.typewrite("\n\nBest regards,\n\n[Your Name]")

方案2:先输入全量文本,再选中目标行加粗

如果需要先输入完整内容再统一格式,可以通过键盘快捷键定位并选中文本:

import pyautogui
import time

time.sleep(2)

# 输入完整邮件文本
full_content = """Dear [Recipient],

I hope this email finds you well. I wanted to let you know that we have some exciting news to share. We recently launched a new product that has received a lot of positive feedback from our customers. The product is designed to help people [feature 1] and [feature 2]. We believe it has the potential to make a real difference in people's lives.

Please let me know if you have any questions or would like to learn more.

Best regards,

[Your Name]"""
pyautogui.typewrite(full_content)

# 定位并加粗第一行目标文本
time.sleep(1)
# 按上箭头回到目标行(根据实际行数调整次数)
for _ in range(3):
    pyautogui.press('up')
# 跳转到行首
pyautogui.press('home')
# 选中整行
pyautogui.hotkey('shift', 'end')
# 加粗
pyautogui.hotkey('ctrl', 'b')

# 定位并加粗第二行目标文本
time.sleep(1)
pyautogui.press('down')
pyautogui.press('home')
pyautogui.hotkey('shift', 'end')
pyautogui.hotkey('ctrl', 'b')

关键注意事项

  • 确保Outlook编辑窗口处于激活状态,可通过pygetwindow库实现自动激活:
    import pygetwindow as gw
    outlook_win = gw.getWindowsWithTitle('Untitled - Message (HTML)')[0]
    outlook_win.activate()
    time.sleep(1)
    
  • 加入time.sleep()避免操作过快导致控件未响应,等待时长可根据实际情况调整。
  • Mac系统需将ctrl替换为command键,快捷键逻辑一致。

内容的提问来源于stack exchange,提问作者Varadhan E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 14:52:26