Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 10-23-2022, 03:29 AM   #1
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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
SylvanKnight is offline   Reply With Quote
Old 10-23-2022, 03:34 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,866
Karma: 22666666
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.
kovidgoyal is online now   Reply With Quote
Advert
Old 10-24-2022, 12:01 AM   #3
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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.
SylvanKnight is offline   Reply With Quote
Old 10-24-2022, 12:03 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,866
Karma: 22666666
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.
kovidgoyal is online now   Reply With Quote
Old 11-21-2022, 06:26 AM   #5
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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>`?
SylvanKnight is offline   Reply With Quote
Advert
Old 11-21-2022, 09:50 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,866
Karma: 22666666
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
kovidgoyal is online now   Reply With Quote
Old 12-03-2022, 09:54 AM   #7
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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
When I upload the plugin into Calibre to test it, I get an error

Code:
ModuleNotFoundError:No module named 'PyPDF2'
I've attached both the zip and the __init__ if anyone is willing to look at/help.
Attached Files
File Type: zip ao3_tagger.zip (592.5 KB, 88 views)
File Type: py __init__.py (8.0 KB, 84 views)
SylvanKnight is offline   Reply With Quote
Old 12-03-2022, 04:56 PM   #8
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,092
Karma: 1948136
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
You can change the part highlighted in blue, so long as you stick to only including letters, numbers and underscores.

Quote:
Originally Posted by SylvanKnight View Post

I call it like

Code:
from PyPDF2 import PdfReader

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
Note: I have not tested whether the above instructions work with your plugin. Will leave that to you.
capink is offline   Reply With Quote
Old 12-03-2022, 05:15 PM   #9
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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.
SylvanKnight is offline   Reply With Quote
Old 12-03-2022, 05:31 PM   #10
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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
SylvanKnight is offline   Reply With Quote
Old 12-03-2022, 07:11 PM   #11
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,584
Karma: 22735033
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.
Attached Files
File Type: zip ao3_tagger.zip (595.6 KB, 83 views)
Doitsu is offline   Reply With Quote
Old 12-04-2022, 04:46 AM   #12
SylvanKnight
Member
SylvanKnight began at the beginning.
 
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...
SylvanKnight is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 09:16 AM.


MobileRead.com is a privately owned, operated and funded community.