Created
October 25, 2015 17:22
-
-
Save arvidj/ef1d2df0ba3db2d0118f to your computer and use it in GitHub Desktop.
Import a powerpoint presentation to an Anki2 collection
This file contains hidden or 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 sys | |
import getopt | |
from os.path import join, split, isfile, isdir | |
from os import mkdir | |
from pptx import Presentation | |
from anki import Collection | |
# Inspiration: https://github.com/nheinric/addToAnki/blob/master/add_to_anki-2.0.12.py | |
def extract(ppt_path, col_path, deck_name): | |
print("Open {0}".format(ppt_path)) | |
prs = Presentation(ppt_path) | |
col = Collection(col_path) | |
col_media_path = join(split(col_path)[0], 'collection.media') | |
if not isdir(col_media_path): | |
mkdir(col_media_path) | |
did = col.decks.id(deck_name) | |
col.decks.select(did) | |
basic_model = col.models.byName('Basic') | |
basic_model['did'] = did | |
col.models.save( basic_model ) | |
col.models.setCurrent( basic_model ) | |
print("Deck has "+str(col.cardCount())+" cards") | |
# text_runs will be populated with a list of strings, | |
# one for each text run in presentation | |
i = 0 | |
for slide in prs.slides: | |
print(i) | |
i += 1 | |
img_idx = 1 | |
text_runs = [] | |
imgs = [] | |
for shape in slide.shapes: | |
if shape.has_text_frame: | |
for paragraph in shape.text_frame.paragraphs: | |
for run in paragraph.runs: | |
print("\t" + run.text) | |
print("\t" + '-----') | |
text_runs.append(run.text) | |
if hasattr(shape, 'image'): | |
img = shape.image | |
img_filename = join(col_media_path, "{0}.{1}-{2}".format(i, img_idx, img.filename)) | |
f = open(img_filename, 'w') | |
f.write(img.blob) | |
print("\twrote {0}".format(img_filename)) | |
img_idx += 1 | |
imgs.append(img_filename) | |
print("Make a new Card for slide {0}".format(i)) | |
fact = col.newNote() | |
fact['Front'] = '' | |
for img in imgs: | |
fact['Front'] += '<img src="{0}" />'.format(img) | |
fact['Back'] = "<br />".join(text_runs) | |
# .decode('utf-8') | |
col.addNote(fact) | |
col.save() | |
col.close() | |
# Boiler-plate: | |
class Usage(Exception): | |
def __init__(self, msg): | |
self.msg = msg | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
try: | |
try: | |
opts, args = getopt.getopt(argv[1:], "h", ["help"]) | |
except getopt.error, msg: | |
raise Usage(msg) | |
if len(args) != 3: | |
raise Usage("Expects three arguments: input powerpoint, collection file, deck name") | |
extract(args[0], args[1], args[2]) | |
except Usage, err: | |
print >>sys.stderr, err.msg | |
print >>sys.stderr, "for help use --help" | |
return 2 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment