Comments (5)
Thanks @remiberthoz for the super-detailed help!
The ability to read existing .apkg
files is not on the roadmap for genanki in the near future. I think it can be done, but would be a decent amount of work and I would worry about issues like certain fields getting corrupted / mishandled in the translation from .apkg
to Python object back to .apkg
(for example, there are already certain LaTeX-related fields that genanki does not handle but instead just hardcodes defaults; we would need to fix support for these fields first if we want to correctly propagate them out of an .apkg
that sets them).
I do agree that this feature could be worth doing. If someone wants to implement it I will work with them to get it merged, or if I have a windfall of free time I may implement it myself. It's not a huge task but needs to be properly implemented, tested, documented, and have corner cases worked out.
Closing this issue for now to keep the tracker tidy but will re-open if someone is actively working on it.
from genanki.
Sure!
You will need python's sqlite3
module. I suggest that you manually extract the database from the apkg, for simplicity when getting started (maybe you can automate it later).
Rename your .apkg
as a .zip
file, and extract it somewhere. Find a file called collection.anki2
, rename it to database.sqlite
, and place it somewhere easy to access from your python code.
Then in python:
# Load the sqlite module and create a cursor to the database
import sqlite
conn = sqlite3.connect('database.sqlite')
c = conn.cursor()
You now have access to the database from this python cursor (c
).
The collection database is organised in several tables. The one you are interested in is named notes
, it contains multiple columns: of interest to you are id
(note ID), guid
(note GUID), flds
(note fields, i.e. the displayed text), sfld
(note sorting field, i.e. a value with which the notes are sorted in anki).
You can query the table for this fields with:
# Query columns of interest and print each row
# You can query all columns using `*` in place of field names
for row in c.execute('SELECT id, guid, flds, sfld FROM notes ORDER BY sfld):
# Each `row` will be a tuple with as many entries as field queried
print(row)
id = row[0]
guid = row[1]
flds = row[2]
sfld = row[3]
# (You cannot manipulate the database directly here)
You can't modify the database directly with this SELECT
query. To do so you would need to UPDATE
. But I think the cleanest thing would be to extract data of interest into a file (i.e CSV, JSON, Yaml) and then use another script with genanki to create a new deck.
More on the SQLite3 python module: https://docs.python.org/3/library/sqlite3.html.
Now about implementing a read feature within genanki, that would be for @kerrickstaley to decide. It's a convenient feature but I'm not sure it's the purpose of this tool.
from genanki.
I managed to achieve my goal, so thanks for the help.
I think I'll keep this issue open until Kerrick responds, as there is definitely merit in creating this feature
from genanki.
If you know some SQL, you can open the apkg file as a zip. Inside you will find a sqlite database with all the data for this deck. The database is not intuitive to navigate, but that would get you started quickly.
from genanki.
I know a tiny bit about SQL, but have never used SQL from Python.
Please can you provide me with a relevant section of code within Anki, or some docs/example on how to open and query the apkg?
Thanks
Also, if this library can save as an SQL database, there's no reason for it to not to go in the other direction, so this is a feature that could be worth implementing
from genanki.
Related Issues (20)
- Support new Anki package format HOT 1
- How to import media file from memory instead of from hdd? HOT 4
- zipfile append mode for decks involving large media files HOT 3
- Field contained the following invalid HTML tags. Make sure you are calling html.escape() if your field data isn't already HTML-encoded: <= nums.length <= 10<sup> <= nums[i] <= 10<sup> <= target <= 10<sup> HOT 1
- Crash on empty/null field HOT 3
- Subdecks HOT 2
- SVG code fails regex "invalid HTML tags" HOT 1
- Empty list of fields in my note
- Add PyPI description
- Could we use an existing Card Type when creating a deck? HOT 2
- How to add cards to an existing deck HOT 2
- Cards created with genanki dont sync correctly to the mobile version HOT 4
- Genanki-generated deck is twice as heavy as it should be HOT 1
- Description is missing on pypi HOT 1
- Apkg cannot be importet to Anki Desktop: JsonError { info: "decoding modesl: JsonError" } HOT 1
- Add decription what does my_package.media_files = ['sound.mp3', 'images/image.jpg'] HOT 3
- if it possible to append to an existing apkg file? HOT 2
- Media files are not being extracted HOT 7
- Use File in a Note
- builtin_models.CLOZE_MODEL is missing the "Extra" field HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from genanki.