Created
August 22, 2021 10:34
-
-
Save JackKuo-tw/88c6781da1f5815195259be70e7ffa42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import sys | |
from datetime import datetime | |
import opencc | |
if len(sys.argv) < 2: | |
quit("Usage: python3 " + sys.argv[0] + " PATH_TO_anki2_FILE") | |
else: | |
ANKI2_PATH = sys.argv[1] | |
con = sqlite3.connect(ANKI2_PATH) | |
cur = con.cursor() | |
converter = opencc.OpenCC("s2twp.json") | |
ids = [] | |
old_text = [] | |
new_text = [] | |
for row in cur.execute("select id, flds from notes where 1"): | |
ids.append(row[0]) | |
old_text.append(row[1]) | |
new_text.append(converter.convert(row[1])) | |
for key, nt in enumerate(new_text): | |
print(key) | |
cur.execute( | |
"update notes set flds=:new_text, mod=:now where id=:id;", | |
{"new_text": nt, "id": ids[key], "now": int( | |
datetime.now().timestamp())}, | |
) | |
con.commit() | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment