Calibre Python



Latest version

Released:

April 2020: Calibre is under constant development, and since I wrote this a couple years ago I see options that I didn't notice back then.There are configurations for adding an SSL certificate, setting up user accounts, and disabling guest access, which makes it a really secure browser. Jul 25, 2020 First, you need to download Calibre's command line tools. You can follow instructions on Calibre's site to download, or download through package managers: # on Ubuntu sudo apt-get install calibre # on MacOS brew install calibre On MacOS, the command line tools may not be added to your path.

Search local Calibre XML export, lookup results online.

Project description


Accessing specific books and fields

calibre_search loads the .csv Calibre library export file in its main folder on import.

This library is represented by the books object:


The Book object fields can be accessed by dot field name:


Searching

General search

You can search any specific field using .search(search_term, field_name):


Filtering on title

You can partially match title field using .intitle:


Filtering on tag

You can partially match tags using books.intags:


Chaining filters

The .intitle and .intags filters can be chained:


Release historyRelease notifications | RSS feed

0.3.7

0.3.6

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for calibre-search, version 0.3.7
Filename, sizeFile typePython versionUpload dateHashes
Filename, size calibre_search-0.3.7-py2.py3-none-any.whl (11.4 kB) File type Wheel Python version py2.py3 Upload dateHashes
Filename, size calibre search-0.3.7.tar.gz (9.7 kB) File type Source Python version None Upload dateHashes
Close

Hashes for calibre_search-0.3.7-py2.py3-none-any.whl

Hashes for calibre_search-0.3.7-py2.py3-none-any.whl
AlgorithmHash digest
SHA2563ca6cc037530562375d447c5a9a3fa171640c52d594f0a7bb5e9de361343abdc
MD5dad91491f702520e84d352e346fafbf0
BLAKE2-256e7a880752df975399ad90ada5c3b053506241c48d3379778eb4f16bc7dff85c4
Close

Hashes for calibre search-0.3.7.tar.gz

Hashes for calibre search-0.3.7.tar.gz
AlgorithmHash digest
SHA256823143c65a083b4e7e6d3eb6f39ca24a6125d207a7a680988761697c436bf696
MD5fd7307fa56f1e5f43835ada5b40597d6
BLAKE2-256309d121c2eb5275001f47f58af80320b19ec3ad65b3717da90267b5d5498ec72

The Search & replace tool in the editor support a function mode.In this mode, you can combine regular expressions (see All about using regular expressions in calibre) witharbitrarily powerful Python functions to do all sorts of advanced textprocessing.

In the standard regexp mode for search and replace, you specify both aregular expression to search for as well as a template that is used to replaceall found matches. In function mode, instead of using a fixed template, youspecify an arbitrary function, in thePython programming language. This allowsyou to do lots of things that are not possible with simple templates.

Techniques for using function mode and the syntax will be described by means ofexamples, showing you how to create functions to perform progressively morecomplex tasks.

Automatically fixing the case of headings in the document¶

Here, we will leverage one of the builtin functions in the editor toautomatically change the case of all text inside heading tags to title case:

For the function, simply choose the Title-case text (ignore tags) builtinfunction. The will change titles that look like: <h1>someTITLE</h1> to<h1>SomeTitle</h1>. It will work even if there are other HTML tags insidethe heading tags.

Your first custom function - smartening hyphens¶

The real power of function mode comes from being able to create your ownfunctions to process text in arbitrary ways. The Smarten Punctuation tool inthe editor leaves individual hyphens alone, so you can use the this function toreplace them with em-dashes.

To create a new function, simply click the Create/edit button to create a newfunction and copy the Python code from below.

Every Search & replace custom function must have a unique name and consist of aPython function named replace, that accepts all the arguments shown above.For the moment, we wont worry about all the different arguments toreplace() function. Just focus on the match argument. It represents amatch when running a search and replace. Its full documentation in availablehere.match.group() simply returns all the matched text and all we do is replacehyphens in that text with em-dashes, first replacing double hyphens andthen single hyphens.

Use this function with the find regular expression:

And it will replace all hyphens with em-dashes, but only in actual text and notinside HTML tag definitions.

The power of function mode - using a spelling dictionary to fix mis-hyphenated words¶

Often, e-books created from scans of printed books contain mis-hyphenated words– words that were split at the end of the line on the printed page. We willwrite a simple function to automatically find and fix such words.

Use this function with the same find expression as before, namely:

And it will magically fix all mis-hyphenated words in the text of the book. Themain trick is to use one of the useful extra arguments to the replace function,dictionaries. This refers to the dictionaries the editor itself uses tospell check text in the book. What this function does is look for wordsseparated by a hyphen, remove the hyphen and check if the dictionary recognizesthe composite word, if it does, the original words are replaced by the hyphenfree composite word.

Note that one limitation of this technique is it will only work formono-lingual books, because, by default, dictionaries.recognized() uses themain language of the book.

Auto numbering sections¶

Dedrm Python 3

Now we will see something a little different. Suppose your HTML file has manysections, each with a heading in an <h2> tag that looks like<h2>Sometext</h2>. You can create a custom function that willautomatically number these headings with consecutive section numbers, so thatthey look like <h2>1.Sometext</h2>.

Use it with the find expression:

Place the cursor at the top of the file and click Replace all.

This function uses another of the useful extra arguments to replace(): thenumber argument. When doing a Replace All number isautomatically incremented for every successive match.

Another new feature is the use of replace.file_order – setting that to'spine' means that if this search is run on multiple HTML files, the filesare processed in the order in which they appear in the book. SeeChoose file order when running on multiple HTML files for details.

Auto create a Table of Contents¶

Finally, lets try something a little more ambitious. Suppose your book hasheadings in h1 and h2 tags that look like<h1id='someid'>SomeText</h1>. We will auto-generate an HTML Table ofContents based on these headings. Create the custom function below:

And use it with the find expression:

Run the search on All text files and at the end of the search, awindow will popup with “Debug output from your function” which will have theHTML Table of Contents, ready to be pasted into toc.html.

The function above is heavily commented, so it should be easy to follow. Thekey new feature is the use of another useful extra argument to thereplace() function, the data object. The data object is a Pythondict that persists between all successive invocations of replace() duringa single Replace All operation.

Python

Another new feature is the use of call_after_last_match – setting that toTrue on the replace() function means that the editor will callreplace() one extra time after all matches have been found. For this extracall, the match object will be None.

This was just a demonstration to show you the power of function mode,if you really needed to generate a Table of Contents from headings in your book,you would be better off using the dedicated Table of Contents tool inTools → Table of Contents.

The API for the function mode¶

All function mode functions must be Python functions named replace, with thefollowing signature:

When a find/replace is run, for every match that is found, the replace()function will be called, it must return the replacement string for that match.If no replacements are to be done, it should return match.group() which isthe original string. The various arguments to the replace() function aredocumented below.

The match argument¶

The match argument represents the currently found match. It is aPython Match object.Its most useful method is group() which can be used to get the matchedtext corresponding to individual capture groups in the search regularexpression.

The number argument¶

The number argument is the number of the current match. When you runReplace All, every successive match will cause replace() to becalled with an increasing number. The first match has number 1.

The file_name argument¶

This is the filename of the file in which the current match was found. Whensearching inside marked text, the file_name is empty. The file_name isin canonical form, a path relative to the root of the book, using / as thepath separator.

The metadata argument¶

This represents the metadata of the current book, such as title, authors,language, etc. It is an object of class calibre.ebooks.metadata.book.base.Metadata.Useful attributes include, title, authors (a list of authors) andlanguage (the language code).

The dictionaries argument¶

This represents the collection of dictionaries used for spell checking thecurrent book. Its most useful method is dictionaries.recognized(word)which will return True if the passed in word is recognized by the dictionaryfor the current book’s language.

The data argument¶

This a simple Python dict. When you runReplace all, every successive match will cause replace() to becalled with the same dict as data. You can thus use it to store arbitrarydata between invocations of replace() during a Replace alloperation.

The functions argument¶

Calibre Python Version

The functions argument gives you access to all other user definedfunctions. This is useful for code re-use. You can define utility functions inone place and re-use them in all your other functions. For example, suppose youcreate a function name MyFunction like this:

Calibre Python Free

Then, in another function, you can access the utility() function like this:

You can also use the functions object to store persistent data, that can bere-used by other functions. For example, you could have one function that whenrun with Replace All collects some data and another function thatuses it when it is run afterwards. Consider the following two functions:

Calibre Python Library

Debugging your functions¶

You can debug the functions you create by using the standard print()function from Python. The output of print will be displayed in a popup windowafter the Find/replace has completed. You saw an example of using print()to output an entire table of contents above.

Choose file order when running on multiple HTML files¶

When you run a Replace all on multiple HTML files, the order inwhich the files are processes depends on what files you have open for editing.You can force the search to process files in the order in which the appear bysetting the file_order attribute on your function, like this:

file_order accepts two values, spine and spine-reverse which causethe search to process multiple files in the order they appear in the book,either forwards or backwards, respectively.

Having your function called an extra time after the last match is found¶

Sometimes, as in the auto generate table of contents example above, it isuseful to have your function called an extra time after the last match isfound. You can do this by setting the call_after_last_match attribute on yourfunction, like this:

Appending the output from the function to marked text¶

When running search and replace on marked text, it is sometimes useful toappend so text to the end of the marked text. You can do that by settingthe append_final_output_to_marked attribute on your function (note that youalso need to set call_after_last_match), like this:

Suppressing the result dialog when performing searches on marked text¶

You can also suppress the result dialog (which can slow down the repeatedapplication of a search/replace on many blocks of text) by settingthe suppress_result_dialog attribute on your function, like this:

More examples¶

More useful examples, contributed by calibre users, can be found in thecalibre E-book editor forum.