You can try the following code (copy/past into the module editor):
Code:
from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre_plugins.action_chains.actions.base import ChainAction
class CoverFromBook(ChainAction):
name = 'Cover from book'
support_scopes = True
def run(self, gui, settings, chain):
db = gui.current_db.new_api
book_ids = chain.scope().get_book_ids()
for book_id in book_ids:
fmts = db.formats(book_id, verify_formats=False)
paths = list(filter(None, [db.format_abspath(book_id, fmt) for fmt in fmts]))
mi = metadata_from_formats(paths)
if mi.cover_data:
cdata = mi.cover_data[-1]
if cdata is not None:
db.set_cover({book_id: cdata})