Unzipping, editing and zipping ODT documents in python

This is a python script created with a single purpose: to test unzipping of OpenOffice.org (LibreOffice) word processor .odt document file, searching in its contents for a certain text and replacing it with a substitute, and, eventually, zipping it all back together to form a new .odt document.

If you copy and past this text, remember, that HTML formatting may spoil the code, so please check it for any introduced mistakes. Especially since this is python with its indentation issues.

 

#!/usr/bin/python
#
# Just a test script
# Demonstrates unzipping, editing and zipping of ODT documents in python
# Source ODT file "in.odt" shall exist in "/tmp"
# If ODT file contains string token, it will be replaced with string replacement
#

Read the rest of this entry »


Unzip and zip OpenOffice.org ODT files

As you probably know, ODT files of OpenOffice.org (LibreOffice) are in essence just ZIP archives.

It is very easy to unpack them. But it is not so easy to create them. If you simply zip the contents of unpacked ODT and change the extension to .odt, it will not work.

The key here is that the mimetype file shall come into the archive first.

Two simple bash scripts are given here to extract and then pack back the ODT files.

 

Script odt_extract (provide  .odt file name and desired destination directory as command line arguments):

#!/bin/bash
echo "Extracting ODT file $1 to directory $2 ..."
unzip "$1" -d "$2"   #1>/dev/null

 

Script odt_compress (provide  source directory and desired .odt file name as command line arguments):

#!/bin/bash
echo "Compressing contents of directory $1 into ODT compatible document $2 ... "
# Move into document source directory
cd "$1"
# Special zipping procedure to create correctly structured ODT file
# Remember that if the provided resulting file path is relative, it shall be so in respect to the current working directory!
# First comes the "mimetype" file ...
zip -0 -X "$2" mimetype   #1>/dev/null
# ... then everything else follows
zip -r "$2" * -x mimetype   #1>/dev/null

Using SQLite database with OpenOffice.org

To use SQLite database with OpenOffice.org or LibreOffice:

First install (as root):
libsqliteodbc
unixodbc-bin

Then run:
ODBCCreateDataSourceQ4 (create data source for “user”)
ODBCManageDataSourcesQ4 (if needed)

In Office:

First register the database in Base, then go on using it in Calc and Writer

Base:
Connect to an existing database -> ODBC

Calc:
View -> Data sources

Writer:
View -> Data sources


Bash script to play .m4v video and .ul audio together

So you have a lot of .m4v video files and a lot of .ul audio files with the same names. That, for example, might be a collection of  CCTV records, where the name of the file is the date and time of when the record was made. Now you would like to play them.

Create a bash script file in ~/bin, /usr/bin or similar directory. Paste the code inside. Add execution rights. Then run the script with .m4v video  file name as the only argument. The script will figure out name of the audio file on its own and afterwards it will play it all with a bunch of various parameters.

Let’s call it playit:

#!/bin/bash
 videoFile=$1
 audioFile="${videoFile%.*}.ul"
 mplayer -fps 25 -vf denoise3d=0:0:100:0 -cache 16000 -framedrop $videoFile -audiofile $audioFile -audio-demuxer rawaudio -rawaudio format=0x0007:channels=1:rate=8000

Later you can play all video files in the current directory with their accompanying audio files by means of another script, let’s call this one playall:

#!/bin/bash
 for i in *.m4v; do
    playit $i
 done

And another improvement. A script to look into all subdirectories of the current directory and play media found there. Useful if you have your packs of audio &  video files stored in timestamped directories. Let’s call this one playdirs:

#!/bin/bash
rootDir=$(pwd)
for subDir in */; do
cd $subDir
playall
cd $rootDir
done

And then yet another one. Play only files only in a single directory. That will be playdir:

#!/bin/bash
 subDir=$1
 rootDir=$(pwd)
 cd $subDir
 playall
 cd $rootDir

Installing XBM on Ubuntu

Installing XBMC (soon to be Kodi) media center software on an Ubuntu based system (Ubuntu, Mint etc. ):

  • sudo apt-get install python-software-properties pkg-config
  • sudo apt-get install software-properties-common
  • sudo add-apt-repository ppa:team-xbmc/ppa
  • sudo apt-get update
  • sudo apt-get install xbmc

Upgrading it some time later:

  • sudo apt-get update
  • sudo apt-get install xbmc

Removing it eventually, if the need will be:

  • sudo apt-get update
  • sudo apt-get remove xbmc*
  • sudo apt-get purge xbmc*

Convert ODT to PDF

Use LibreOffice (OpenOffice.org) in command line mode to convert all of the ODT files in the directory to PDF files in subdirectory PDF:

  • libreoffice --headless --convert-to pdf --outdir PDF ./*.odt

P.S.

Use ./*.odt instead of simply *.odt, because otherwise the program will consider file names beginning with “-” to be an argument and will fail.


Monitor directory with inotify

You can use inotifywait to watch a directory (or a file for that matter) for changes and run some action when they happen.

A sample script, which watches for a new file to be created and prints only its name:

#!/bin/bash

inotifywait -m -e create --format %f /tmp/mytestdir | while read FILE
do
  echo "$FILE"
done

Follow

Get every new post delivered to your Inbox.