Basic Printing Support: how to print notes from oldest to newest?

Hi I’m using this addon and loving but it seems that it sorts the cards alphabetically using the first letter of a field

(I made this real card days ago. today I made 26 cards from A to Z just to check it)

So I need to sort the cards from old to new bc I study and do the cards right from books and this way I preserve the following of a content and I can print instead of doing a review material

Someone recently asked me to modify the add-on to do the same. Here is the updated package:

1 Like

Here’s an updated link:

Hi, I’m getting this error message when trying to use the print.ankiaddon. I’m using Anki 23.10.1

Traceback (most recent call last):
  File "C:\Users\...\print\__init__.py", line 32, in onPrint
    QStandardPaths.writableLocation(QStandardPaths.DesktopLocation), "print.html"
AttributeError: type object 'QStandardPaths' has no attribute 'DesktopLocation'

Did you update the add-on to the latest version?

Has anyone tried this recently? Im copying the new update into the python file and then when i go to print it tells me theres a problem with my add on

What new update? And what files are you copying where? This add-on should automatically prompt you for an update when one is available.

Maybe you can try this, replace the code in " init.py" file:

import os
import re

from anki.cards import CardId
from anki.decks import DeckId
from anki.utils import ids2str
from aqt import mw
from aqt.qt import *
from aqt.utils import openLink

config = mw.addonManager.getConfig(name)

def sortedCids(did: DeckId) → list[CardId]:
dids = [did]
for name, id in mw.col.decks.children(did):
dids.append(id)
return mw.col.db.list(
“”"
select c.id from cards c where did in %s order by c.id"“”
% ids2str(dids)
)

def onPrint():
path = os.path.join(
QStandardPaths.writableLocation(QStandardPaths.StandardLocation.DesktopLocation),
“print.html”,
)

try:
    did = mw.col.decks.current()["id"]  # Get current deck ID
    ids = sortedCids(did)
except Exception as e:
    QMessageBox.information(None, "Error", f"Could not retrieve deck ID: {str(e)}")
    return

def esc(s: str) -> str:
    # Remove repeated question in answer if exists
    s = re.sub(r"\[\[type:[^]]+\]\]", "", s)  # Remove type answer
    return s

buf = open(path, "w", encoding="utf8")
buf.write("<html><head>" + '<meta charset="utf-8">' + mw.baseHTML() + "</head><body>")
buf.write(
    """<style>

img { max-width: 100%; }
tr { page-break-inside:avoid; page-break-after:auto }
td { page-break-after:auto; }
td { border: 1px solid #ccc; padding: 1em; }
.playImage { display: none; }

“”"
)
first = True

mw.progress.start(immediate=True)
for j, cid in enumerate(ids):
    if j % config.get("cardsPerRow", 2) == 0:  # Default to 2 if config is missing
        if not first:
            buf.write("</tr>")
        else:
            first = False
        buf.write("<tr>")
    
    c = mw.col.get_card(cid)
    qatxt = c.render_output(True, False).answer_text
    qatxt = mw.prepare_card_text_for_display(qatxt)
    cont = '<td width="{1}%"><center>{0}</center></td>'.format(
        esc(qatxt), 100 / config["cardsPerRow"]
    )
    buf.write(cont)
    
    if j % 50 == 0:
        mw.progress.update(f"Cards exported: {j + 1}")
buf.write("</tr>")
buf.write("</table></body></html>")
mw.progress.finish()
buf.close()
openLink(QUrl.fromLocalFile(path))

Add action to menu

q = QAction(mw)
q.setText(“Print”)
q.setShortcut(QKeySequence(“Shift+P”))
mw.form.menuTools.addAction(q)
q.triggered.connect(onPrint)