Beware, this is kind of ugly because Calibre doesn't provide math functions (especially
modulo) that would make life so much easier.
Code:
program:
# This program calculate a reading time per hour for a ebook
# Adjustments:
# w_min -> minimum words per minute
# w_max -> maximum words per minute
w_min=250;
w_max=450;
# readingtime per hour=60
p_time=60;
# Needs a custom column for word count (#words)
# change it to your field name
words=raw_field('#words');
time1=format_number(divide(words,multiply(w_max, p_time)), '{0:.2f}');
time2=format_number(divide(words,multiply(w_min, p_time)), '{0:.2f}');
# Unfortunately Calibre doesn't provide a simple way to convert a float
# into a time string so I do it the really REALLY hard way.
# get the hour BUT take into consideration that Python rounds
# the numbers automatically!
# Subtracting 0.49 should always yield the correct value
# (e.g. 9.0 -> 8.51 -> rounded up to 9)
# To the minutes I add a leading '0' for easier conversion later,
# otherwise times are displayed as 4:4h instead of 4:04h.
rt1_hour=format_number(subtract(time1, 0.49), '{0:.0f}');
rt1_minute=format_number(multiply(subtract(time1,rt1_hour),60), '0{0:.0f}');
rt2_hour=format_number(subtract(time2, 0.49), '{0:.0f}');
rt2_minute=format_number(multiply(subtract(time2,rt2_hour),60), '0{0:.0f}');
# It can also happen that the hour value is -0 (!) if the
# reading time is < 30min.
rt1_hour = re(rt1_hour, '-', '');
rt2_hour = re(rt2_hour, '-', '');
# only the last 2 chars are needed from the minutes.
readingtime=strcat(rt1_hour,':', substr(rt1_minute, -2, 0), 'h - ', rt2_hour, ':', substr(rt2_minute,-2,0), 'h');