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 09-02-2023, 10:50 PM   #1
dragonscythe
Junior Member
dragonscythe began at the beginning.
 
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!
dragonscythe is offline   Reply With Quote
Old 09-02-2023, 11:26 PM   #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,869
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
mi.set('#genre', whatever)
kovidgoyal is offline   Reply With Quote
Advert
Old 09-03-2023, 07:27 AM   #3
dragonscythe
Junior Member
dragonscythe began at the beginning.
 
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!')
dragonscythe is offline   Reply With Quote
Old 09-03-2023, 08:23 AM   #4
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 dragonscythe View Post
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)
chaley is offline   Reply With Quote
Old 09-03-2023, 09:21 AM   #5
dragonscythe
Junior Member
dragonscythe began at the beginning.
 
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
dragonscythe is offline   Reply With Quote
Advert
Old 09-06-2023, 06:15 PM   #6
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 930
Karma: 1177583
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Quote:
Originally Posted by dragonscythe View Post
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
A piece of advise: besides reading the documentation, search for other plugins that does similar operations, so you can check how they do stuff. This is very helpful.
thiago.eec is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 11:14 PM.


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