09-02-2023, 10:50 PM | #1 |
Junior Member
Posts: 3
Karma: 10
Join Date: Sep 2023
Device: PC
|
Assigning custom column metadata
Hi! I have been reading through the documentation and searching these threads, but I'm still a bit stuck. Please help
I've got this so far (this is working): from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.book.base import Metadata from calibre.library import db # Path to PDF file pdf_path = 'E:\\CalibreClaudeProject\\testPDFExtraction\\In The Ruins.pdf' # Get metadata from PDF mi = Metadata(get_metadata(open(pdf_path, 'rb'), stream_type='pdf')) # Add custom metadata if needed mi.title = 'Custom Title' mi.authors = ['Author Name'] mi.publisher = 'Some Publisher' mi.tags = ['myTag', 'myTag2'] # Open connection to Calibre library db = db('F:\\CalibreLibrary').new_api # Add PDF file to Calibre db.add_books([(mi, {'PDF': pdf_path})]) print('PDF added to Calibre!') What I want to do is learn how to assign custom fields. I have a Genre/Setting column with the lookup/search name #genre. I'm sure this is simple, but I've hit a wall in this and will be incredibly thankful for any help! |
09-02-2023, 11:26 PM | #2 |
creator of calibre
Posts: 44,409
Karma: 23977332
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
mi.set('#genre', whatever)
|
Advert | |
|
09-03-2023, 07:27 AM | #3 |
Junior Member
Posts: 3
Karma: 10
Join Date: Sep 2023
Device: PC
|
Thank you! Unfortunately, when I add that and run the script using calibre-debug, it still pulls the PDF in and doesn't throw any errors, but it doesn't fill the Genre/Setting (#genre) field.
Did I put that line in the right place? Do I need to do anything to pull in custom columns first? from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.book.base import Metadata from calibre.library import db # Path to PDF file pdf_path = 'E:\\CalibreClaudeProject\\testPDFExtraction\\In The Ruins.pdf' # Get metadata from PDF mi = Metadata(get_metadata(open(pdf_path, 'rb'), stream_type='pdf')) # Add custom metadata if needed mi.title = 'Custom Title' mi.authors = ['Author Name'] mi.publisher = 'Some Publisher' mi.tags = ['myTag', 'myTag2'] mi.set('#genre', 'whatever') # Open connection to Calibre library db = db('F:\\CalibreLibrary').new_api # Add PDF file to Calibre db.add_books([(mi, {'PDF': pdf_path})]) print('PDF added to Calibre!') |
09-03-2023, 08:23 AM | #4 | |
Grand Sorcerer
Posts: 11,953
Karma: 7225107
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
A simpler solution is to add the genre data after you add the book. The newly added book has all the fields defined by the library. Something like this (this code is untested): Code:
# Open connection to Calibre library db = db('F:\\CalibreLibrary').new_api # Add PDF file to Calibre ids, dups = db.add_books([(mi, {'PDF': pdf_path})]) genre_list = [whatever1, whatever2, etc] # genre_list must be a list or tuple val_map = {ids[0]: genre_list} # val_map must be a dict db.set_field('#genre', val_map) |
|
09-03-2023, 09:21 AM | #5 |
Junior Member
Posts: 3
Karma: 10
Join Date: Sep 2023
Device: PC
|
Wow! I have some learning to do to understand the pieces here, but that worked perfectly!
Thank you SO much, chaley!! Thank you both, again, for the replies |
Advert | |
|
09-06-2023, 06:15 PM | #6 | |
Wizard
Posts: 1,030
Karma: 1189271
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
|
Quote:
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
assigning news articles to custom column [kobo] | jmgant108 | Devices | 5 | 08-01-2022 05:55 PM |
Add Read/Unread metadata from custom column to metadata jacket | allanahk | Library Management | 4 | 11-12-2018 03:10 PM |
Metadata editor: Custom column order | Divingduck | Calibre | 4 | 11-20-2015 07:02 AM |
Is there a way to add the metadata AND also the filename in a custom column? | LadyKate | Library Management | 5 | 10-10-2014 01:13 PM |
Metadata Plugboard Data in a Custom Column | nynaevelan | Library Management | 2 | 01-10-2012 12:54 PM |