引子

上一周没得空去更新,这周就水篇博客,先介绍背景,我的主力阅读器是kindle paperwhite4,但是呢,前一阵子用书伴网的那个整理标注的书见网站,网站反映有问题(当然在后来,发邮件给作者,作者很负责,也是很快修复了,我也不知道作者怎么搞的,好厉害),于是索性想了一下,ai这么发达,不如和ai合作搞出来一个整理kindle标注的软件,本地使用,用起来也方便整理,最后的效果也不错。下面放两张图片,分别是整理笔记的目录页和正文部分,当然我的图床访问会比较慢,看不到图片也不影响阅读,嘻嘻。这次分享的两个程序都是和ai一起做的,分别是整理的py软件和自动化运行py软件的bat文件。

image-20250514225038541

image-20250514225142754

开始

首先需要部署python环境,这个去官网下载就可以啦。

然后这两个程序可以分别命名kindle_clippings_ultimate.py和run_ultimate_converter.bat 🆗,将其和您的kindle–documents–My Clippings.txt放在一个文件夹里。

接下来,🆗我们开始吧

kindle_clippings_ultimate.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from datetime import datetime
from collections import defaultdict
import os
import argparse
import sys

def parse_kindle_date(date_str):
"""万能日期解析器"""
try:
# 处理中文星期和上下午
date_str = (date_str.replace("星期一", "Monday")
.replace("星期二", "Tuesday")
.replace("星期三", "Wednesday")
.replace("星期四", "Thursday")
.replace("星期五", "Friday")
.replace("星期六", "Saturday")
.replace("星期日", "Sunday")
.replace("星期天", "Sunday")
.replace("上午", "AM")
.replace("下午", "PM"))

# 转换格式:2024年12月10日Monday AM8:59:16 → %Y年%m月%d日%A %p%I:%M:%S
return datetime.strptime(date_str, "%Y年%m月%d日%A %p%I:%M:%S")
except Exception as e:
print(f"⚠️ 日期解析异常({date_str}),使用当前时间替代 | 错误:{str(e)}")
return datetime.now()

def parse_clippings_file(file_path):
"""解析Kindle剪贴文件"""
try:
# 自动检测编码
for encoding in ['utf-8-sig', 'utf-16', 'gb18030']:
try:
with open(file_path, 'r', encoding=encoding) as f:
content = f.read()
break
except UnicodeDecodeError:
continue
else:
raise ValueError("无法确定文件编码")

content = content.replace('\ufeff', '').replace('\r\n', '\n')
entries = [e.strip() for e in content.split('==========') if e.strip()]
clippings = []

pattern = re.compile(
r'^(.*?)\n- 您在第 (\d+) 页(位置 #([\d-]+))的(标注|笔记) \| 添加于 (.*?)\n\n(.*)$',
re.DOTALL
)

for entry in entries:
match = pattern.match(entry)
if match:
title, page, location, clip_type, date_str, content = match.groups()
clippings.append({
'title': title.strip(),
'type': '标注' if '标注' in clip_type else '笔记',
'page': page.strip(),
'location': location.strip(),
'date': parse_kindle_date(date_str.strip()),
'content': content.strip()
})

return clippings

except Exception as e:
print(f"❌ 文件解析失败:{str(e)}")
sys.exit(1)

def generate_markdown(clippings, output_path):
"""生成Markdown文件"""
# 确保输出目录存在
os.makedirs(os.path.dirname(output_path) or '.', exist_ok=True)

clippings.sort(key=lambda x: x['date'], reverse=True)

books = defaultdict(list)
book_last_date = {}

for clip in clippings:
books[clip['title']].append(clip)
if clip['title'] not in book_last_date or clip['date'] > book_last_date[clip['title']]:
book_last_date[clip['title']] = clip['date']

sorted_books = sorted(books.items(),
key=lambda item: book_last_date[item[0]],
reverse=True)

with open(output_path, 'w', encoding='utf-8') as f:
f.write("# 📚 Kindle读书笔记(按最后阅读时间排序)\n\n")
f.write(f"> 导出时间:{datetime.now().strftime('%Y-%m-%d %H:%M')}\n\n")

f.write("## 📋 书籍目录(从新到旧)\n")
for i, (title, _) in enumerate(sorted_books, 1):
safe_title = re.sub(r'[^\w\s-]', '', title)
last_date = book_last_date[title].strftime('%Y-%m-%d')
f.write(f"{i}. [{title}](#{safe_title.lower().replace(' ', '-')}) *(最后标注:{last_date})*\n")
f.write("\n---\n\n")

for title, clips in sorted_books:
safe_title = re.sub(r'[^\w\s-]', '', title)
f.write(f'<a id="{safe_title.lower().replace(" ", "-")}"></a>\n')
f.write(f"## 📖 {title}\n")
f.write(f"*最后标注时间:{book_last_date[title].strftime('%Y-%m-%d %H:%M')}*\n\n")

for clip in clips:
icon = '🔖' if clip['type'] == '标注' else '✏️'
f.write(f"### {icon} {clip['type']}(第{clip['page']}页·位置 {clip['location']})\n")
if clip['type'] == '标注':
f.write(f"> {clip['content']}\n")
else:
f.write(f"{clip['content']}\n")
f.write(f"\n*标注时间:{clip['date'].strftime('%Y-%m-%d %H:%M')}*\n")
f.write("---\n\n")

def main():
parser = argparse.ArgumentParser(description='Kindle剪贴转换终极版')
parser.add_argument('input_file', help='My Clippings.txt文件路径')
parser.add_argument('--output', '-o', default='Kindle_Notes.md', help='输出文件路径')
args = parser.parse_args()

print("⏳ 正在解析Kindle剪贴文件...")
clippings = parse_clippings_file(args.input_file)
print(f"✅ 找到 {len(clippings)} 条笔记(来自 {len(set(c['title'] for c in clippings))} 本书)")

print("🔄 正在生成Markdown文件...")
generate_markdown(clippings, args.output)
print(f"🎉 转换完成:{os.path.abspath(args.output)}")

if __name__ == '__main__':
main()

run_ultimate_converter.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@echo off
:: 终极修复版批处理文件
:: 请保存为ANSI编码

chcp 65001 >nul
title Kindle笔记转换终极版
color 0A

echo.
echo [INFO] Kindle剪贴转换工具(终极修复版)
echo ------------------------------------------
echo.

:: 检查Python
where python >nul 2>nul || (
echo [错误] 需要Python环境
echo 正在打开Python官网...
start https://www.python.org/downloads/
pause
exit /b 1
)

:: 检查输入文件
if not exist "My Clippings.txt" (
echo [错误] 未找到My Clippings.txt
echo 请确认:
echo 1. 文件名必须完全匹配(含.txt扩展名)
echo 2. 文件必须与此bat在同一目录
pause
exit /b 1
)

:: 生成安全的输出文件名(避免特殊字符)
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set year=%%a
set month=%%b
set day=%%c
)
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (
set hour=%%a
set minute=%%b
)

set "output_file=Kindle笔记_%year%-%month%-%day%_%hour%-%minute%.md"

:: 执行转换
echo [状态] 正在转换笔记...
python "%~dp0kindle_clippings_ultimate.py" "My Clippings.txt" -o "%output_file%"

if %errorlevel% equ 0 (
echo.
echo [成功] 转换完成!
echo 输出文件:%output_file%
timeout /t 2
if exist "%output_file%" (
start "" "%output_file%"
) else (
echo [警告] 输出文件未生成,请检查目录权限
)
) else (
echo.
echo [错误] 转换失败
pause
)

exit /b

结语

ai确实在我们的生活中发挥了越来越多的作用,可以帮助我们学习、给出生活经验、为生产增效等等,这也看出来我们学习在变得相对便捷,所以大家也要抓紧机会好好学习提升自己哇,希望这个kindle标注整理程序和bat执行文件可以帮到大家。