View Single Post
Old 06-19-2020, 07:17 PM   #1
wolf123
Member
wolf123 began at the beginning.
 
Posts: 16
Karma: 10
Join Date: Jun 2020
Device: nook simple touch
Issue parsing HTML tags

Hi guys,

I am developing an (edit book) plugin, and I don't seem to be able to parse html tags. My `main.py` file looks like so:
Code:
import lxml.etree
from PyQt5.Qt import QAction, QInputDialog

# The base class that all tools must inherit from
from calibre.gui2.tweak_book.plugin import Tool

from calibre import force_unicode
from calibre.gui2 import error_dialog
from calibre.ebooks.oeb.polish.container import OEB_DOCS, serialize


class MyTool(Tool):
    name = 'my-tool'
    allowed_in_toolbar = True
    allowed_in_menu = True

    def create_action(self, for_toolbar=True):
        ac = QAction(get_icons('icon/icon.png'), 'My Tool', self.gui)

        if not for_toolbar:
            self.register_shortcut(ac, 'my-tool',
                                   default_keys=('Ctrl+Shift+A',))

        ac.triggered.connect(self.run)
        return ac

    def run(self):
        container = self.current_container

        # iterate over book files
        for name, media_type in container.mime_map.items():
            if media_type in OEB_DOCS:

                self.my_method(container.parsed(name))

                container.dirty(name)

                
    def my_method(self, root):

        for el in root.iter('div'):
            el.attrib['class'] = 'my_class'

            # debug
            print('found a div tag')
I would expect divs to get a 'my_class' class, or at least to read debug lines printed in the terminal; but I don't.

What am I getting wrong here?

Many thanks!
wolf123 is offline   Reply With Quote