-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanki_to_pdf.py
executable file
·30 lines (26 loc) · 1.29 KB
/
anki_to_pdf.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
import re
import os
import sys
def bold(text):
return ('<span style="font-weight: bold; font-size: 20px; padding-bottom: 5px; display: block; border-bottom: 1px solid gray;">{}</span>'.format(text))
def fix_img_srcs(text):
pattern = re.compile(r"<img src='((.)*)' \/>")
ret = re.sub(pattern, r"<img src='" + os.environ['HOME'] + r"/.local/share/Anki2/User%201/collection.media/\1' />", text)
return ret
def main():
with open(sys.argv[1], "r") as doc:
pattern = re.compile(r"^\"(((\n)*((?!\").)*(\n)*)*)\";\"(((\n)*(((?!\").)*)(\n)*)*)\"$", re.MULTILINE | re.DOTALL)
data = doc.read()
itera = re.finditer(pattern, data)
content = ""
for card in itera:
div_def = ("<div style='border: 1px solid gray; background-color:#f9f4f4; margin:10px; padding:10;'>")
front = (fix_img_srcs(bold(card.group(1))) + "<br />")
back = (fix_img_srcs(card.group(6)) + "<br />")
end_div = ("</div><br />")
content+= div_def + front + back + end_div
with open("/tmp/tmp_anki_html_file.html", "w") as out:
out.write(content)
os.system("wkhtmltopdf --enable-local-file-access /tmp/tmp_anki_html_file.html " + sys.argv[2] + " && rm -f /tmp/tmp_anki_html_file.html")
if __name__ == '__main__':
main()