![]() |
#1 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
|
calibre python script to update custom fields (help)
I'm working on converting over from goodreads to calibre. So far, so good. One of the things I'd like is two custom fields: Read and Stars to match whether I've read the book or not and the stars I give the book.
I was able to make the fields and can set them manually in Calibre no problem. I can read in and match books pretty well, but I can't seem to store the custom fields via python. Here's what I have (in brief): Code:
metadata = calibre_db.get_metadata(id) stars = metadata.get_user_metadata("#stars", True) stars["#value#"] = 4 metadata.set_user_metadata("#stars", stars) read = metadata.get_user_metadata("#read", True) read["#value#"] = True metadata.set_user_metadata("#read", read) |
![]() |
![]() |
![]() |
#2 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
|
Since it's not much longer, though this would benefit from a more complete test script:
Code:
# Run with calibre-debug -e calibre_filler.py import calibre.library LIBRARY = "calibre/andy" calibre_db = calibre.library.db(LIBRARY).new_api # calibre_books = get_calibre_books() metadata = calibre_db.get_metadata(291) stars = metadata.get_user_metadata("#stars", True) stars["#value#"] = 7 # values are 0 to 10 read = metadata.get_user_metadata("#read", True) read["#value#"] = True print(f" Update metadata from [{stars['#value#']}] [{read['#value#']}]") metadata.set_user_metadata("#stars", stars) metadata.set_user_metadata("#read", read) |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,364
Karma: 27230406
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
calibre_db.set_metadata or set_field
|
![]() |
![]() |
![]() |
#4 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
|
Thanks. I'm still unclear on how this works.
https://manual.calibre-ebook.com/db_api.html Is this the only documentation? set_metadata requires an "mi" (Metadata object), which I assume is the *full* metadata for the book (because it says that you need to specify None if you don't want a given field replaced). But I don't see where the Metadata object itself is documented, so I'm not sure how to index into it to edit just the user-defined fields. Similarly, set_field asks for a "mapping of book id's to values". What is that? And what is the "name" of the field if it's a user-defined field? |
![]() |
![]() |
![]() |
#5 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
:param book_id_to_val_map: Mapping of book_ids to values that should be applied. The name of the field is the lookup name, #something for custom columns. |
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
BTW:
|
![]() |
![]() |
![]() |
#7 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Moderator Notice
Moved to the development subforum |
![]() |
![]() |
![]() |
#8 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jan 2025
Device: none
|
I see. Looks like a quick test of set_field does work well. Thanks!
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Calibre Access in Python Script | KissMeKiwi | Development | 5 | 04-14-2024 05:42 AM |
mass update/mass-editing as a list custom fields | alkmaar | Library Management | 7 | 08-30-2019 01:24 PM |
3.34 calibre-debug runs the script code under Python 3 | EbokJunkie | Conversion | 2 | 11-20-2018 07:43 PM |
Calibre beta available that fully integrates custom fields | chaley | Calibre | 206 | 10-03-2010 02:28 AM |
Calibre custom news feed and python help. | harrynewman | Calibre | 4 | 10-08-2009 09:26 AM |