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 11-05-2020, 07:25 AM   #1
sigizmund
Member
sigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheese
 
Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
Question 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()
Thanks!
sigizmund is offline   Reply With Quote
Old 11-05-2020, 07:48 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,864
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
https://manual.calibre-ebook.com/db_api.html

https://manual.calibre-ebook.com/develop.html
kovidgoyal is offline   Reply With Quote
Old 11-05-2020, 07:54 AM   #3
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by sigizmund View Post
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()
Thanks!
Yes, you can do what you want. Use the calibre-debug command, and in particular calibre-debug -e. The entire calibre api is available.

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)
You execute it with
Code:
calibre-debug -e all_tags.py PATH-TO-LIBRARY
chaley is offline   Reply With Quote
Old 11-05-2020, 07:59 AM   #4
sigizmund
Member
sigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheese
 
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)?
sigizmund is offline   Reply With Quote
Old 11-05-2020, 08:06 AM   #5
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,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')
kovidgoyal is offline   Reply With Quote
Old 11-05-2020, 08:14 AM   #6
sigizmund
Member
sigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheesesigizmund can extract oil from cheese
 
Posts: 24
Karma: 1088
Join Date: Dec 2009
Device: Pocketbook && Android
Got it - thanks for help!
sigizmund is offline   Reply With Quote
Old 04-21-2021, 10:36 AM   #7
Xwang
Connoisseur
Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.
 
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.
Xwang is offline   Reply With Quote
Old 04-21-2021, 11:07 AM   #8
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,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
kovidgoyal is offline   Reply With Quote
Old 04-21-2021, 11:11 AM   #9
Xwang
Connoisseur
Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.
 
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?
Xwang is offline   Reply With Quote
Old 04-21-2021, 11:21 AM   #10
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,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
kovidgoyal is offline   Reply With Quote
Old 04-21-2021, 11:30 AM   #11
Xwang
Connoisseur
Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.Xwang ought to be getting tired of karma fortunes by now.
 
Posts: 77
Karma: 2136220
Join Date: Sep 2012
Device: none
OK, thanks again
Xwang is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 04:10 AM.


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