11-05-2020, 07:25 AM | #1 |
Member
Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
Using Calibre Python API example
Hey folks,
I am trying to understand if I can write a Python code running not as a plugin which will be manipulating Calibre without UI. Example: add book to the specified Calibre library. Obviously starting calibredb command is one option, but I'm interested if I can just `import` some libraries and manipulate database in-process. In pseudocode: Code:
import calibre.database as db database = db.OpenDb('/path/to/library') database.AddBook('/path/to/book.epub') database.Close() |
11-05-2020, 07:48 AM | #2 |
creator of calibre
Posts: 43,864
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
|
11-05-2020, 07:54 AM | #3 | |
Grand Sorcerer
Posts: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
This example prints all the tags in a library, sorted. Code:
from calibre.utils.icu import sort_key def init_cache(library_path): from calibre.db.backend import DB from calibre.db.cache import Cache backend = DB(library_path) cache = Cache(backend) cache.init() return cache cache = init_cache(library_path = sys.argv[1]) all_tags = sorted(list(cache.all_field_names('tags')), key=sort_key) for t in all_tags: print(t) Code:
calibre-debug -e all_tags.py PATH-TO-LIBRARY |
|
11-05-2020, 07:59 AM | #4 |
Member
Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
Thanks folks! Are there any examples of set up of how I can set up development environement for this (ideally using something like pyvenv so it depends less on external configuration)?
|
11-05-2020, 08:06 AM | #5 |
creator of calibre
Posts: 43,864
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
There's no external configuration and no need for pyenv. Write your script, run it with calibre-debug -e. If your script requires python packages that are not bundles with calibre, simply add the path to them at the top of the script with
sys.path.append('/path/to/wherever your python package is') |
11-05-2020, 08:14 AM | #6 |
Member
Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
|
Got it - thanks for help!
|
04-21-2021, 10:36 AM | #7 |
Connoisseur
Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
Is it possible to use this method even if an instance of calibre is already running or is it necessary (or better) to close calibre before running calibre-debug?
I'm going to use it in a shell script and I'm sure that I will not be doing any modification in calibre while the shell script will be running. |
04-21-2021, 11:07 AM | #8 |
creator of calibre
Posts: 43,864
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
depends on what you plan to do with calibre-debug. Any kind of database/library access should not be done while calibre is running. Anything else should mostly be fine
|
04-21-2021, 11:11 AM | #9 |
Connoisseur
Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
Since I would like to modify a metadata, I suppose it is better to do that when calibre is not running, then. Is it correct?
|
04-21-2021, 11:21 AM | #10 |
creator of calibre
Posts: 43,864
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
if youa re modifying metadata in a file use ebook-meta.exe if you are modifying it in the calibre library use calibredb.exe
|
04-21-2021, 11:30 AM | #11 |
Connoisseur
Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
|
OK, thanks again
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Problems using Calibre 3.15.0 Database API | einnordlicht | Development | 2 | 01-06-2018 09:28 PM |
Is there any Python API for file conversion? | avin | Development | 0 | 12-27-2016 08:11 AM |
Calibre API for Mobi conversion | Sebadub | Development | 4 | 07-03-2013 11:59 AM |
Calibre update breaks Calibre API | TechnoCat | Recipes | 5 | 01-28-2013 11:25 PM |
calibre's new plugin API | kovidgoyal | Plugins | 26 | 05-07-2011 02:43 PM |