Quote:
Originally Posted by dragonscythe
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!')
|
You must provide the definition of the custom column, getting the definition from field_metadata, before setting the value. This isn't the easiest thing to do.
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)