Skip to content

Commit

Permalink
csv2apkg
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaisilong committed Oct 6, 2024
1 parent 9f78e4b commit 16536bb
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
12 changes: 12 additions & 0 deletions genanki.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Anki

## genanki: A Library for Generating Anki Decks

```bash
git clone [email protected]:zhaisilong/genanki.git

cd genanki
pip install -e .

csv2apkg test.csv
```
2 changes: 2 additions & 0 deletions genanki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
from .builtin_models import BASIC_OPTIONAL_REVERSED_CARD_MODEL
from .builtin_models import BASIC_TYPE_IN_THE_ANSWER_MODEL
from .builtin_models import CLOZE_MODEL

from .csv2apkg import csv2apkg
90 changes: 90 additions & 0 deletions genanki/csv2apkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from typing import Literal, Optional
import pandas as pd
import fire
from pathlib import Path
from tqdm.auto import tqdm
import genanki
import time

# 定义Anki卡片的模型
MODEL = genanki.Model(
2559383000,
'Basic (zhaisilong)',
fields=[
{'name': 'Front', 'font': 'Arial'},
{'name': 'Back', 'font': 'Arial'}
],
templates=[
{
'name': 'ZCard-1',
'qfmt': '{{Front}}',
'afmt': '{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}',
},
],
css='.card {\n font-family: Arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n',
)

# 主程序
def csv2apkg(filename: str, title: Optional[str] = None, sep: str = ',', mode: Literal['single', 'double'] = 'double'):
csv_path = Path(filename)

# 检查文件是否为 .csv 格式
assert csv_path.suffix == ".csv", "Input file must be a .csv file"

if title and isinstance(title, str):
pass
elif csv_path.stem:
title = csv_path.stem
else:
title = 'Deck'


# 生成 .apkg 文件路径
apkg_path = csv_path.parent / (csv_path.stem + ".apkg")

# 读取 CSV 文件
if mode == 'double':
df = pd.read_csv(csv_path, sep=sep)
elif mode == 'single':
df = pd.read_csv(csv_path, header=None, names=["question"])
df['answer'] = " " # 保持空格 否则就会导入失败
else:
raise ValueError("Invalid mode: csv format error")

# 创建 Anki Deck
deck = genanki.Deck(
deck_id=int(time.time()), # 使用当前时间戳作为 Deck ID
name=title
)

# 逐行遍历 CSV 并创建 Anki Note
with tqdm(total=len(df)) as pbar:
for _, row in df.iterrows():
# 检查行是否包含 'question' 和 'answer' 列
if 'question' in row and 'answer' in row:
# 假设 question 和 answer 本身是 HTML 内容
question_html = str(row['question']) # 使用 HTML 格式的字符串
answer_html = str(row['answer']) # 使用 HTML 格式的字符串

note = genanki.Note(
model=MODEL,
fields=[question_html, answer_html]
)
# 将 Note 添加到 Deck
deck.add_note(note)
else:
print(f"Skipping row: Missing 'question' or 'answer' in {row}")

pbar.update(1)

# 将 Deck 写入 .apkg 文件
genanki.Package(deck).write_to_file(apkg_path)

print(f"Anki deck saved to {apkg_path}")

def main():
fire.Fire(csv2apkg)

# 调用Fire模块,支持命令行调用
if __name__ == '__main__':
main()
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@
'anki',
'flashcards',
'memorization',
])
],
entry_points={
'console_scripts': [
'csv2apkg=genanki.csv2apkg:main', # 定义 csv2apkg 命令行入口
],
}
)

5 changes: 5 additions & 0 deletions test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
question,answer
123,123
222,sddds
1232,1232131
234121,6777777
Binary file added test_single.apkg
Binary file not shown.
5 changes: 5 additions & 0 deletions test_single.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
123123
12313
123dwad
12cs
asdadsas

0 comments on commit 16536bb

Please sign in to comment.