10-23-2022, 03:29 AM | #1 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
How to include a Python module with the plugin
Back with another question for something I can't quite figure out.
I have the functionality I want (for the most part), but my plugin uses PyPDF2 to go through PDF text. When I tried to import the plugin to test it, I got the following error: calibre, version 6.0.0 ERROR: Unhandled exception: <b>ModuleNotFoundError</b>:No module named 'PyPDF2' Code:
calibre 6.0 embedded-python: True Windows-10-10.0.19043 Windows ('64bit', 'WindowsPE') ('Windows', '10', '10.0.19043') Python 3.10.1 Windows: ('10', '10.0.19043', '', 'Multiprocessor Free') Interface language: None Successfully initialized third party plugins: Edit Book plugin demo (1, 0, 0) && FanFicFare (4, 16, 0) && Goodreads (1, 7, 1) && Kindle hi-res covers (0, 5, 0) Traceback (most recent call last): File "calibre\gui2\preferences\plugins.py", line 325, in add_plugin File "calibre\customize\ui.py", line 476, in add_plugin File "calibre\customize\ui.py", line 64, in load_plugin File "calibre\customize\zipplugin.py", line 304, in load File "importlib\__init__.py", line 126, in import_module File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "calibre\customize\zipplugin.py", line 203, in exec_module File "calibre_plugins.dummy2.__init__", line 9, in <module> ModuleNotFoundError: No module named 'PyPDF2' I'm assuming that's because PyPDF2 isn't in Calibre. My question is now 1) What PDF tools are in Calibre so that I can use those instead of PyPDF2, or 2) How do I include PyPDF2 with my plugin? Thanks in advance |
10-23-2022, 03:34 AM | #2 |
creator of calibre
Posts: 44,303
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Simply include it in the plugin zip file. Although there are builtin tools in calibre to do the things pypdf2 does, but faster and more robustly. As for what they are it depends onw hat you want to do, its not a single package.
|
Advert | |
|
10-24-2022, 12:01 AM | #3 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
There is a fanfiction hosting site called Archive of Our Own. One of the features of the site is that you can download PDF, MOBI, and EPUB (as well as other) versions of individual works. My wife has over the years downloaded several hundred works this way. When you download a MOBI or EPUB version, it has appropriate metadata embedded in (tags, published date, place in a series, etc.). However, she has downloaded PDF versions, which contains none of this. All of the same information is usually on the first two pages of the PDF. I'm just trying to grab all the information in those first few pages, sort it out appropriately, and put it into the appropriate categories as its imported into Calibre if it's an AO3 PDF.
|
10-24-2022, 12:03 AM | #4 |
creator of calibre
Posts: 44,303
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
If you want to extract text from the pdf calibre includes the pdftohtml and pdfttotext binaries form the poppler project, use those. See pdftohtml.py in the calibre source for examples of how to use.
|
11-21-2022, 06:26 AM | #5 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
Once I've included the module in the plugin, how do I call it inside the plugin? Is it
`from <plugin name> import <module>`? |
Advert | |
|
11-21-2022, 09:50 AM | #6 |
creator of calibre
Posts: 44,303
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Assuming its a single file module you will import it just like any other module in your plugin
|
12-03-2022, 09:54 AM | #7 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
I'm trying to include PyPDF2 in my plugin. Maybe I'm doing something terribly wrong.
I call it like Code:
from PyPDF2 import PdfReader Code:
ModuleNotFoundError:No module named 'PyPDF2' |
12-03-2022, 04:56 PM | #8 |
Wizard
Posts: 1,134
Karma: 1954142
Join Date: Aug 2015
Device: Kindle
|
Looking at the zip file, your plugin does not have an import name. You create an import name by creating an empty text file in the root of your plugin with the following name:
Code:
plugin-import-name-ao3_tagger.txt
After adding the import name as explained above, you should modify the import statement to include your import name as follows: Code:
from calibre_plguins.ao3_tagger.PyPDF2 import PdfReader
|
12-03-2022, 05:15 PM | #9 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
Same error still
calibre, version 6.9.0 ERROR: Unhandled exception: <b>ModuleNotFoundError</b>:No module named 'PyPDF2' calibre 6.9 embedded-python: True Windows-10-10.0.19045-SP0 Windows ('64bit', 'WindowsPE') ('Windows', '10', '10.0.19045') Python 3.10.1 Windows: ('10', '10.0.19045', 'SP0', 'Multiprocessor Free') Interface language: None Successfully initialized third party plugins: Edit Book plugin demo (1, 0, 0) && FanFicFare (4, 18, 0) && Goodreads (1, 7, 1) && Kindle hi-res covers (0, 5, 0) Traceback (most recent call last): File "calibre\gui2\preferences\plugins.py", line 325, in add_plugin File "calibre\customize\ui.py", line 479, in add_plugin File "calibre\customize\ui.py", line 64, in load_plugin File "calibre\customize\zipplugin.py", line 307, in load File "importlib\__init__.py", line 126, in import_module File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "calibre\customize\zipplugin.py", line 206, in exec_module File "calibre_plugins.ao3_tagger.__init__", line 13, in <module> File "calibre_plugins.ao3_tagger.__init__", line 25, in AO3PdfTagger File "calibre\customize\zipplugin.py", line 206, in exec_module File "calibre_plugins.ao3_tagger.PyPDF2.__init__", line 10, in <module> File "calibre\customize\zipplugin.py", line 206, in exec_module File "calibre_plugins.ao3_tagger.PyPDF2._encryption ", line 34, in <module> ModuleNotFoundError: No module named 'PyPDF2' I've tried having the import inside and outside of the class. |
12-03-2022, 05:31 PM | #10 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
I realize that having files to download to see what they're doing isn't fair to ask of people, so I put it into a GitHub repo
https://github.com/Sylvan-Knight/ao3_tagger |
12-03-2022, 07:11 PM | #11 |
Grand Sorcerer
Posts: 5,635
Karma: 23191067
Join Date: Dec 2010
Device: Kindle PW2
|
Apparently, the error messages are caused by some modules in the PyPDF2 folder who reference the PyPDF2 folder, e.g. _encryption.py.
If you remove "PyPDF2." from these modules, you won't get any error messages, but the plugin doesn't seem to work. |
12-04-2022, 04:46 AM | #12 |
Member
Posts: 12
Karma: 10
Join Date: Sep 2022
Device: kindle
|
That's what it was. I did a find/replace and now it's at least running in Calibre. Now to debug so it does what I want it to do...
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Use calibre ebook-convert as a Python Module | apducer | Development | 2 | 10-24-2019 01:46 PM |
Calibre - Python No module named magick | BigBird | Calibre | 7 | 10-02-2016 05:01 PM |
Import module not in bundled Python | Toxaris | Plugins | 11 | 02-01-2016 01:55 AM |
python error: No module named MySQLdb | stgabriel | Development | 1 | 03-10-2013 02:13 PM |
No Module name Tkinter on plugin import | foghat | Plugins | 1 | 11-11-2010 07:11 PM |