Skip to content

Instantly share code, notes, and snippets.

@sjrogers
Last active April 25, 2024 01:08
Show Gist options
  • Save sjrogers/673e22bc93835dc8c9a37ef6794383db to your computer and use it in GitHub Desktop.
Save sjrogers/673e22bc93835dc8c9a37ef6794383db to your computer and use it in GitHub Desktop.
How To Add "Last Accessed" Column In Calibre

How To Add "Last Accessed" Column In Calibre

Calibre does not keep track of the last time you opened a book, but your filesystem does. Or at least, it kinda does. Thankfully you can use Calibre template functions and custom columns to make use of this information.

Step 1: Create Template Function

  1. Go to Preferences -> Advanced -> Template Functions
  2. Select the Function field at the bottom left of the window
  3. Type last_accessed and give it some documentation if you like
  4. Set Argument count to -1 because the required arguments have everything we need
  5. In the Program code box on the right, paste this code (you may have to fiddle with the indentation):
  def evaluate(self, formatter, kwargs, mi, locals):
    from os import stat
    from time import ctime
    book_paths = [f['path'] for f in mi.get('format_metadata', {}).values()]
    atimes = map(lambda f: stat(f).st_atime, book_paths)
    most_recent = max(atimes)
    return ctime(most_recent)
  1. Click Create at the bottom center (or Replace if you messed up the first time!)
  2. Click Apply at the bottom right

Step 2: Add Custom Column

  1. Go to Preferences -> Interface -> Add your own columns
  2. Click Add custom column at the bottom
  3. Click Formats in the Quick Create line at the top of the dialog window
  4. In Lookup name enter last_accessed
  5. In Column heading enter Last Accessed (or Last read, etc.)
  6. Give it a description if you like
  7. In the Template field, enter {:'last_accessed()'}
  8. Set the Sort/search column by dropdown to Date
  9. Click OK to close the custom column dialog
  10. Click Apply to finish adding the column

Restart and enjoy!

The changes you made will take effect when Calibre restarts. If it did not prompt you already, just close and reopen it. You can now sort books by the last date you watched them!

@AndrewCheck
Copy link

The column I've created seems to be only visible in the library currently loaded, is there a way to make custom column visible to all existing libraries ? Thank you Andrea

@LeducH
Copy link

LeducH commented Jul 7, 2021

create advice. You maybe need to mention that the change of date only applies after a calibre restart . Meaning when you open a file and close it. It remains the same data in the table. but after a restart the new data is applied.

In any case good work. I mean greatly explained.

@isjamesalive
Copy link

Nice, thank you.

@mikefowler
Copy link

Exactly what I was looking for, thanks @sjrogers! For those who want a different date format, here's a small modification. The following outputs a column text like "Sep 20, 2022 at 3:07pm"

def evaluate(self, formatter, kwargs, mi, locals):
    from os import stat
    from time import ctime, strftime
    from datetime import datetime
    
    book_paths = [f['path'] for f in mi.get('format_metadata', {}).values()]
    atimes = map(lambda f: stat(f).st_atime, book_paths)
    most_recent = max(atimes)
    t = datetime.fromtimestamp(most_recent)

    # Reference https://strftime.org/ for strftime cheatsheet 
    return t.strftime("%b %d, %Y at %I:%M %p")

@Luciushy
Copy link

but it cant work on 6.2.1.

@prashantk100
Copy link

Thanks a ton ,caliber sort column by last access time /opened time /modified time is working , I tested with caliber 6.71
and the last_accessed column is coming , I have used code given by @mikefowler and is working fantastic . another advantage is , the reverse sorting on last_accessed column will give you books which you never opened.,

@Luciushy
Copy link

Thanks a ton ,caliber sort column by last access time /opened time /modified time is working , I tested with caliber 6.71 and the last_accessed column is coming , I have used code given by @mikefowler and is working fantastic . another advantage is , the reverse sorting on last_accessed column will give you books which you never opened.,

would you mind showing me how to sort the books by the last-accessed oreder in the calibre 6.7.1.
it dowsnt work foe me.

@Jake985Cole
Copy link

Thank you very much! It works. You're clear to give instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment