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

如何用Python生成可粘贴到Apple Notes的格式化文本?

可行方案

一、独立Python脚本实现(优先)

核心是生成RTF富文本写入剪贴板,Apple Notes原生支持RTF格式粘贴,可保留标题层级、字体样式(加粗/斜体)等。

具体步骤:

  1. 提取Kindle笔记后,将内容映射为RTF格式:
    • 书籍标题设置为加粗+大号字体,用\b\fs24标记开头,\b0取消加粗
    • 标注内容用斜体(\i)或加粗区分,普通笔记用默认格式
    • 用\par实现换行,\pard重置段落格式
  2. 借助rtf库简化RTF内容生成,通过macOS的pbcopy命令将RTF写入剪贴板(pyperclip默认仅支持纯文本,需指定RTF类型)

示例代码:

import subprocess
from rtf import RtfDocument

def build_kindle_rtf(book_title, highlights):
    rtf_doc = RtfDocument()
    # 添加书籍标题(加粗+大号字体)
    rtf_doc.paragraph(book_title, bold=True, font_size=24)
    # 添加标注和笔记
    for idx, item in enumerate(highlights, 1):
        rtf_doc.paragraph(f"标注 {idx}:", bold=True)
        rtf_doc.paragraph(item["content"], italic=True)
        if "note" in item and item["note"].strip():
            rtf_doc.paragraph(f"笔记: {item['note']}")
    return rtf_doc.as_string()

def copy_rtf_to_clipboard(rtf_content):
    # macOS下用pbcopy写入RTF格式内容
    proc = subprocess.Popen(
        ["pbcopy", "-Prefer", "rtf"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE
    )
    proc.communicate(input=rtf_content.encode("utf-8"))

# 模拟提取的Kindle笔记数据
book_title = "1984"
highlights = [
    {"content": "战争即和平,自由即奴役,无知即力量。", "note": "核心口号"},
    {"content": "谁控制了过去,谁就控制了未来;谁控制了现在,谁就控制了过去。"}
]

# 生成并复制RTF到剪贴板
rtf_output = build_kindle_rtf(book_title, highlights)
copy_rtf_to_clipboard(rtf_output)

二、Shortcuts/AppleScript补充方案

如果RTF生成存在兼容问题,可采用混合方案:

  • Python生成标准Markdown格式的笔记内容,写入剪贴板
  • 通过Shortcuts的「转换Markdown为富文本」动作处理后,自动粘贴到Apple Notes
  • 或用AppleScript直接调用Notes应用创建带格式的笔记,Python通过subprocess执行脚本:

示例AppleScript(保存为create_kindle_note.scpt):

on run {rtf_content}
    tell application "Notes"
        set new_note to make new note with properties {name:"Kindle笔记", body:rtf_content}
        activate
    end tell
end run

Python调用代码:

import subprocess

def call_apple_script(rtf_content):
    subprocess.run(
        ["osascript", "create_kindle_note.scpt", rtf_content],
        check=True
    )

内容的提问来源于stack exchange,提问作者Nick-H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 22:13:38