Ubuntu – Auto convert odt to pdf

conversiondocumentlibreofficepdf

I am creating a few documents in Libre office and I have to always send them as .pdf.

but each and every time I forget to export it as pdf , So is there any way to auto convert the .odt document into pdf every time I save the document ?

I have only about 4 docs , I keep making changes on them , So each and every time I make a change and save the odt I need that change to be updated in the corresponding pdf file .

Ps : I understand that unoconv can be used to convert via command line but is there a way to automatically do it ?

Another Ps : I found out that there is something called inotify and inotify-tools and that can be used to trigger events when a file changes . But I have no idea on how to use it .

Best Answer

A quick and dirty example using code from the pyinotify project [http://github.com/seb-m/pyinotify]

You will need to change the WATCHED_DIR to your directory containing ODT files. Also remember to install unoconv first.

# Notifier example from tutorial
#
# See: http://github.com/seb-m/pyinotify/wiki/Tutorial
#
# odtwatcher.py

import os
import pyinotify
import subprocess

WATCHED_DIR = '/tmp/test'

wm = pyinotify.WatchManager()  # Watch Manager
mask = pyinotify.IN_MODIFY

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_MODIFY(self, event):
        fname = event.pathname            
        if os.path.splitext(fname)[1] == '.odt':
            print 'MODIFIED: ', fname
            args = ['unoconv', '-f', 'pdf', fname]
            try:
                subprocess.Popen(args)
            except OSError as e:
                print 'Could not convert file %s to PDF. Error %s' % (fname, str(e))

if __name__ == '__main__':
    handler = EventHandler()
    notifier = pyinotify.Notifier(wm, handler)
    wdd = wm.add_watch(WATCHED_DIR, mask, rec=True)        
    notifier.loop()

Save this as odtwatcher.py and then run it in the background

python odtwatcher.py &