0%

用python制作anki卡组

目标是读取pdf做anki卡组,记录一下鼓捣了一晚上的结果。

准备工作

pdf必须是文字版 扫描版目前的识别率还是太低 即使是用FineReader也还是得手动调整 工作量太大

pdf转word

因为word读取比较方便(可能只是因为我菜) 所以我先用 adobe acrobat 将pdf转成word

word转anki

目标就是 读取正面内容 加tab 然后加背面内容 然后加回车
也正因为如此,背面如此是不能用回车换行的 如果需要换行就加 br 然后anki里勾选允许使用html

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
#文件路径
path = "test.docx"

#读取文件对象
file = docx.Document(path)

#写入文档
outfile = open('output.txt', 'w', encoding='utf-8')

#count
for line in file.paragraphs:
content = line.text #读取内容
if len(content) > 1: #空行或标题行(比如A)不要
if str(line.runs[0].font.color.rgb) == '0000FF': #蓝色字是正面
wordlist = content.split( )
outfile.write('\n') #背面加回车(不过这样的问题是第一行也是空的 要输出后手动删去 不然放进anki会出错)
outfile.write(wordlist[0]+'\t') #正面+tab
i += 1
for otherword in wordlist[1:]:
if len(otherword) > 5:
outfile.write(otherword+'<br>') #背面写入 +换行 为了美观
else:
outfile.write(otherword) #背面不换行写入
else:
outfile.write(content + '<br>')

if i%5 == 0:
print(str(i)+' words completed')

outfile.close()

导入anki

打开anki 导入输出的txt 选择html 看一下设置 就行啦