Anki插件Export deck to html的改造

在Anki中进行复习时,每次只能打开一条笔记。如果积累了很多笔记,有时候会有将它们集中输出成一个pdf进行阅读的想法。Anki插件Export deck to html(安装ID:1897277426)就有这个功能。但是,这个插件目前存在以下问题:

1、Anki升级为版本 ⁨24.06.3 (d678e393)⁩后,插件无法正常运行(也许更早的版本就这样,我没试过);

2、插件转pdf的效果不是很好,但转html的效果不错。考虑到html转pdf非常容易(word即可完成,多数浏览器在插件支持下或无需插件也能完成),所以插件的转pdf功能比较鸡肋;

3、笔记中的img标签,在转换为html后,除了“src”属性得以保留,其余的属性会全部丢失。

4、输出的html在每一条笔记前添加了没有用处的前缀“>y”

鉴于上述问题,所以对该插件的主文件ExportDeckToHtml.py进行了修改。具体修改的内容包括:

1、将不再兼容新版Anki的几行代码进行修改和删除,其中包括

1)dialog.exec_()修改为dialog.exec()

2)options = QFileDialog.DontUseNativeDialog删除

3)path = QFileDialog.getSaveFileName( self, "Save File", directory, "All Files (*)", options=options) 修改为path = QFileDialog.getSaveFileName(  self, "Save File", directory, "All Files (*)")

2、修改_setup_ui函数,取消界面上的保存为pdf等元素。

3、修改_export_to_html函数,在处理卡片的html中的img标签时,只将src属性中的路径修改为绝对路径,而src属性之外的其他属性保持不变。

4、修改每条笔记的html,增加笔记序号信息,删掉无用前缀。

修改后的ExportDeckToHtml.py文件内容如下:

from aqt import mw, utils
from aqt.qt import *
from os.path import expanduser, join
from pickle import load, dump

import os
import re
import unicodedata
from .pdfkit import from_string

delimiter = "####"

ascending = "Ascending"
descending = "Descending"
config_file = "export_decks_to_html_config.cfg"


class AddonDialog(QDialog):

    """Main Options dialog"""

    def __init__(self):
        global config_file
        QDialog.__init__(self, parent=mw)
        self.path = None
        self.deck = None
        self.fields = {}
        self.card_orders = [ascending, descending]
        self.order_fn = None
        self.advance_mode = False
        if os.path.exists(config_file):
            try:
                self.config = load(open(config_file, 'rb'))
            except:
                self.config = {}
        else:
            self.config = {}
        self._setup_ui()

    def _handle_button(self):
        dialog = OpenFileDialog()
        self.path = dialog.filename
        if self.path is not None:
            utils.showInfo("Choose file successful.")

    def _handle_load_template(self):
        dialog = OpenFileDialog()
        self.advance_mode = False
        self.template_path = dialog.filename
        if self.template_path is not None and len(self.template_path) > 0:
            utils.showInfo("Choose file successful.")
            self.template_label.setText(self.template_path)

    def _setup_ui(self):
        """Set up widgets and layouts"""
        layout = QGridLayout()
        layout.setSpacing(10)

        deck_label = QLabel("Choose deck")

        # deck name
        self.deck_selection = QComboBox()
        deck_names = sorted(mw.col.decks.allNames())
        current_deck = mw.col.decks.current()['name']
        deck_names.insert(0, current_deck)
        for i in range(len(deck_names)):
            if deck_names[i] == 'Default':
                deck_names.pop(i)
                break
        self.deck_selection.addItems(deck_names)
        self.deck_selection.currentIndexChanged.connect(self._select_deck)
        layout.addWidget(deck_label, 1, 0, 1, 1)
        layout.addWidget(self.deck_selection, 1, 1, 1, 2)

        export_dir = self.config.get('export_dir', expanduser("~/Desktop"))
        self.export_dir = QLineEdit(export_dir)

        field_label = QLabel('Sort')
        self.field_selection = QComboBox()
        fields = self._select_fields(self.deck_selection.currentText())
        if self.deck_selection.currentText() in self.config:
            currentField = self.config[self.deck_selection.currentText()].get(
                'field_selection', '')
            if len(currentField) > 0:
                if currentField in fields:
                    fields.remove(currentField)
                    fields.insert(0, currentField)
        self.field_selection.addItems(fields)

        layout.addWidget(field_label, 2, 0, 1, 1)
        layout.addWidget(self.field_selection, 2, 1, 1, 2)

        template_path = ''
        if self.deck_selection.currentText() in self.config:
            template_path = self.config[self.deck_selection.currentText()].get(
                'template_path', '')

        self.template_label = QLabel(template_path)

        # order
        order_label = QLabel('Order')
        self.order_selection = QComboBox()
        orders = self.card_orders[:]
        if self.deck_selection.currentText() in self.config:
            currentOrder = self.config[self.deck_selection.currentText()].get(
                "order_selection", '')
            if len(currentOrder) > 0:
                orders.re
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yivifu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值