Skip to content

Commit 400819c

Browse files
committed
add anki convert utils
1 parent 69f46f9 commit 400819c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

anki_data_builder.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/python -u
2+
# -*- coding: utf-8 -*-
3+
4+
import csv
5+
import sqlite3
6+
import sys
7+
import argparse
8+
9+
__version__ = '1.0.0'
10+
11+
12+
class AnkiCardBuilder:
13+
def __init__(self, sqlite_input_file, csv_output_file):
14+
self.sqlite_input = sqlite_input_file
15+
self.csv_output = csv_output_file
16+
17+
def dump_csv(self,):
18+
conn = sqlite3.connect(self.sqlite_input)
19+
conn.text_factory = str # my current (failed) attempt to resolve this
20+
cursor = conn.cursor()
21+
data = cursor.execute("SELECT front, back FROM cards")
22+
self.__write_csv(data)
23+
24+
def __write_csv(self, items):
25+
with open(self.csv_output, 'w') as file:
26+
27+
c = csv.writer(file, delimiter='\t', lineterminator='\n')
28+
# Add '#' to first field, so Anki treats the line as a comment line.
29+
c.writerow(['# FRONT', 'BACK'])
30+
for item in items:
31+
c.writerow([
32+
item[0],
33+
item[1],
34+
])
35+
36+
37+
if __name__ == '__main__':
38+
"""
39+
Sample usage: ./anki_data_builder.py ~/cards-jwasham-extreme.db ./anki-card.csv
40+
"""
41+
parser = argparse.ArgumentParser(
42+
description='Exports Flash cards sqlite3 data to CSV for Anki import.')
43+
parser.add_argument(
44+
'sqlite_file', help="Full path to the flash cards sqlite3 db file. Include file name.")
45+
parser.add_argument(
46+
'csv_file', help="Full path to the CSV file to save the file to. Include file name.")
47+
args = parser.parse_args()
48+
49+
sqlite_file = args.sqlite_file
50+
csv_file = args.csv_file
51+
anki = AnkiCardBuilder(sqlite_file, csv_file)
52+
anki.dump_csv()

0 commit comments

Comments
 (0)