|
|
Thread Tools | Search this Thread |
11-18-2011, 02:40 PM | #1 |
Member
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
|
How to automatically populate a tag with the decade from the pubdate via python
Hi all.
has anyone written any python code to get the decade from the pubdate year and put it into a custom column? ie 1910's 2000's etc Im going to attempt one but i dont know half the commands for string manipulation Any help appreciated! |
11-18-2011, 03:16 PM | #2 |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Assuming you are writing a custom template function, the following gets the first three digits of the year with a trailing 0. It doesn't check for the 'undefined' date (1/1/0101).
Code:
def evaluate(self, formatter, kwargs, mi, locals): x = (mi.pubdate.year/10)*10 return '%04d'%x |
Advert | |
|
11-18-2011, 03:50 PM | #3 |
Member
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
|
The code works great for me, cheers!
i have never used the mi parameter before can i use this to access any of the metadata for the book? ie mi.tags or mi.#anothercustomcolumn? |
11-19-2011, 04:33 AM | #4 |
Member
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
|
is there any documentation on how to use mi and formatter? i have struggled to find any!
thanks |
11-19-2011, 06:09 AM | #5 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
The only formatter method that is 'public' is Code:
safe_format(self, fmt, kwargs, error_value, book)
Code:
def evaluate(self, formatter, kwargs, mi, locals): x = kwargs.get('pubdate', None) if x is None: return '' x = (x.year/10)*10 return '%04d'%x Code:
from calibre.ebooks.metadata.book.base import Metadata if isinstance(kwargs, Metadata): do what you want to do |
|
Advert | |
|
11-19-2011, 06:41 AM | #6 |
Member
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
|
Thanks Chaley, really appreciate that info!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Automatically add tag if word/phrase found in ebook? | eosrose | Calibre | 3 | 11-16-2011 06:48 AM |
pubdate from PDF Metadata | montymaverick | Library Management | 0 | 11-08-2011 07:29 AM |
Quality (stars) doesn't populate | jeffla376 | Library Management | 5 | 10-03-2011 10:06 PM |
How do I populate a custom field on import? | himitsuhieki | Library Management | 7 | 08-19-2011 03:37 PM |
Using PubDate in print_version of custom news source | mobilereader72 | Calibre | 4 | 05-30-2009 05:52 PM |