-
You want to batch convert .doc to .pdf using the command line on a server without a GUI? Or you need automated .ppt to .swf conversion through cron, a sysvinit service, or a remote web server? Online conversion services such as Zamzar.com and Media-convert.com not working for you? Whichever formats you need to batch convert, PyODConverter is a simple Python script for just this purpose.
On Linux
Make sure you have the headless package installed. For the vanilla OpenOffice.org 2.4, it has the name openoffice.org-headless-2.4.0-9286.i586.rpm.
This shell script demonstrates the full use of PyODConverter for Linux:
#!/bin/bash
# Try to autodetect OOFFICE and OOOPYTHON.
OOFFICE=`ls /usr/bin/openoffice.org2.4 /usr/bin/ooffice /usr/lib/openoffice/program/soffice | head -n 1`
OOOPYTHON=`ls /opt/openoffice.org*/program/python /usr/bin/python | head -n 1`
if [ ! -x "$OOFFICE" ]
then
echo "Could not auto-detect OpenOffice.org binary"
exit
fi
if [ ! -x "$OOOPYTHON" ]
then
echo "Could not auto-detect OpenOffice.org Python"
exit
fi
echo "Detected OpenOffice.org binary: $OOFFICE"
echo "Detected OpenOffice.org python: $OOOPYTHON"
# Reference: http://wiki.services.openoffice.org/wiki/Using_Python_on_Linux
# If you use the OpenOffice.org that comes with Fedora or Ubuntu, uncomment the following line:
# export PYTHONPATH="/usr/lib/openoffice.org/program"
# If you want to simulate for testing that there is no X server, uncomment the next line.
#unset DISPLAY
# Kill any running OpenOffice.org processes.
killall -u `whoami` -q soffice
# Download the converter script if necessary.
test -f DocumentConverter.py || wget http://www.artofsolving.com/files/DocumentConverter.py
# Start OpenOffice.org in listening mode on TCP port 8100.
$OOFFICE "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -norestore -nofirststartwizard -nologo -headless &
# Wait a few seconds to be sure it has started.
sleep 5s
# Convert as many documents as you want serially (but not concurrently).
# Substitute whichever documents you wish.
$OOOPYTHON DocumentConverter.py sample.ppt sample.swf
$OOOPYTHON DocumentConverter.py sample.ppt sample.pdf
# Close OpenOffice.org.
killall -u `whoami` soffice
Xvfb vs -headless
To use the -headless command line parameter, you must use OpenOffice.org 2.3.0 or later with the RPM package openoffice.org-headless-2.3.1-9238.i586.rpm. If you use an older OpenOffice.org version, you will need Xvfb to simulate an X server where one is not available:
!/bin/bash
# Set DISPLAY to something besides :1 (because :1 is the standard display).
DISPLAY=:1000
# Kill any existing virtual framebuffers.
killall -u `whoami` Xvfb
# Start the framebuffer.
Xvfb $DISPLAY -screen 0 800x600x24 &
# Run the OpenOffice.org conversion script above.
ooo-convert.sh
# Clean up
killall -u `whoami` Xvfb
On Windows
On Windows, convert documents as follows. Of course, download PyODConverter and adjust the OpenOffice.org pathnames to match the version of OpenOffice.org you have installed.
Windows users don't have to worry about Xvfb or the headless package.
"C:\Program Files\OpenOffice.org2.4\program\soffice.exe" -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager
"C:\Program Files\OpenOffice.org2.4\program\python" DocumentConverter.py test.odt test.pdf
UPDATE: On Windows XP in OpenOffice.org 2.4, you may need to switch the socket to a pipe if you get the following error:
_main__.com.sun.star.connection.NoConnectException: Connector : couldn't connect to socket (WSANOTINITIALISED, WSAStartup() has not been called)
Supported formats
PyODConverter supports all of the formats that you can import or export manually in OpenOffice.org. They include:
- odt OpenDocument (ODF) Text
- sxw OpenOffice.org version 1 Text
- doc Microsoft Word 97/2000/XP
- rtf Rich Text Format
- txt Plain text
- wpd WordPerfect
- html Web page
- ods OpenDocument (ODF) Spreadsheet
- sxc OpenOffice.org version 1 Spreadsheet
- xls Microsoft Excel 97/2000/XP
- odp OpenDocument (ODF) Presentation
- sxi OpenOffice.org version 1 Presentation
- ppt Microsoft PowerPoint 97/2000/XP
- swf Adobe Flash
To convert .docx, .xlsx, .pptx, substitute OxygenOffice for OpenOffice.org.
To export to .csv, use JODConverter.
Reliability
Especially when batch converting many documents, you should take into consideration that the OpenOffice.org process may crash. If it does, the conversion script will fail too. A reliable method is to start OpenOffice.org, convert one document, tear down the process, and repeat; however, this is slow. You may wish to reuse the OpenOffice.org process, say 10 times, while using error checking to determine whether the process needs restarted prematurely.
JODConverter 3.0 (currently in development) will automatically restart OpenOffice.org in the event of a crash.
PDF printer method
If you just want to generate PDF files, you don't need a Python script, a Basic macro, Java code, or any other kind of programming. Just install a PDF printer such as PDFCreator (Windows) or CUPS-PDF. Then, use the -pt command with the the first argument as the printer name and the second argument as the source document.
Linux:
openoffice.org2.4 -norestore -nofirststartwizard -nologo -headless -pt Cups-PDF sample.ppt
Windows:
"C:\Program Files\OpenOffice.org2.4\program\soffice" -norestore -nofirststartwizard -nologo -headless -pt PDFCreator sample.ppt
Thanks to bikram for this tip.
Converting to image formats
Do you need to convert to .png, .jpg, or .tif? Install ImageMagick. Then, convert whichever format to .pdf. Then, convert the .pdf to the image format like this:
convert sample.pdf sample.png
convert sample.pdf sample.jpg
convert sample.pdf sample.tif
If the document has multiple pages, ImageMagick creates filenames like sample-0.png.
If you prefer a more direct approach, use a script such as unoconv.
Other methods
There are many ways to batch convert files with OpenOffice.org:
- OxygenOffice as a Word 2007 (.docx) importer if you want to convert between OpenXML formats (.docx, .pptx, and .xlsx) and other formats supported by OpenOffice.org
- Windows: Convert Office Open XML files (.docx, .xlsx, .pptx) to OpenDocument (.odt, .ods, .odp) using command line
- Linux: Convert Office Open XML files (.docx, .xlsx, .pptx) to OpenDocument (.odt, .ods, .odp) using command line
- Windows: Convert Office Open XML files (.docx, .xlsx, .pptx) to Microsoft Office binary files (.doc, .xls, .ppt)
- JODConverter robust method using Java
- Moving to OpenOffice: Batch Converting Legacy Documents uses a Basic macro
- ooo2any another Python script

24 comments:
Great blog. But in "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -norestore -nofirststart -nologo -headless &
it's -nofirststartwizard and not -nofirststart
Aidan PangChi: Nice catch! I fixed that and made small updates.
Has anyone an ideo how to convert something like html into mediawiki text?
Thank you very much!
Anonymous: Try HTML::WikiConverter which I use online, but you can download it too.
Hello,
thx for the solution but i don´t want to use the online tool. I want to use my own server. Hay anyone an idea ?
Thank you !
Are you same anonymous? :) If so, the answer is you can download HTML::WikiConverter for use on your own server.
first of all - thank you a lot for the very helpful script - we appreciate it a lot!
When we start OOffice as headless server on a linux machine and use your script on another host (mainly ppt to html) the OO-instance grows endless. Limiting it with ulimit to say 1GByte leads to a crash (alloc error) after a while. Has anyone a hint how we could get around that?
Kindly Your's f.woeckener
This is very nice, you information helped for me but I want a converter program which can convert batch file into shell script file. Please tell me a way for that.
Can use the same script in BROffice ?!
Anonymous: Basically, yes, you can use it in BROffice. You may need to adjust the command line slightly to account for any difference in BROffice's different pathnames.
Please any body tell me a way to extract Text data from the pdf file with java program.
FlexiDoc Server is a product that combines OpenOffice.org, Apache and Wiki in a single. You may givi it a try, look for more info here: www.flexidocserver.com
I am getting following error when using PyODConverter:
Traceback (most recent call last):
File "./DocumentConverter.py", line 144, in module
converter.convert(argv[1], argv[2])
File "./DocumentConverter.py", line 76, in convert
document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(Hidden=True))
__main__.IllegalArgumentException: URL seems to be an unsupported one.
Any clues?
Does it work with OO.o 3.0?
I can't get it working, i get an IO Exception
I have sucessfully used this to convert a ppt to pdf, however when I try to convert to html I get "ERROR! ErrorCodeIOException 2074". Has anyone used this to convert ppt to html? Do I need to do something different when specifing the "output file" since it should create a number of files to render the ppt in html?
As a followup to my previous post about converting ppt to html - boy am I feeling foolish! I haven't fully backtracked to see what exactly I did to get things working, but I have had success. I think it may be because I didn't have necessary additional OpenOffice packages installed (I added the SDK, draw, and graphicfilter to my installation).
Hello
Anyone can help me with following errors?
/usr/lib/openoffice.org/program/soffice.bin X11 error: Can't open display:
Set DISPLAY environment variable, use -display option
or check permissions of your X-Server
(See "man X" resp. "man xhost" for details)
error opening security policy file /usr/lib/xserver/SecurityPolicy
Traceback (most recent call last):
File "DocumentConverter.py", line 13, in ?
import uno
ImportError: No module named uno
Traceback (most recent call last):
File "DocumentConverter.py", line 13, in ?
import uno
ImportError: No module named uno
soffice: no process killed
Could not init font path element unix/:7100, removing from list!
FreeFontPath: FPE "built-ins" refcount is 2, should be 1; fixing.
Crirus: The first error about DISPLAY means you either need to use Xvfb or use -headless
I am ..I spent three days figuring this out
I use OO 2.3
Whatever I try to launch soffice it keeps bugging me about X Display
Any more thoughts?
Can anyone point me to a clean instalation of OOo 2.4 headless on a centos 5?
I spent enough figuring out on bad versions...
Thanks
Crirus: Clean copies of OpenOffice.org 2.4 are available from http://download.openoffice.org/2.4.2/index.html
This is great. I'm having an issue though, xls files with multiple worksheets only convert the first one. Any idea how to get around that?
MScappa: Sorry, I am not sure. Try asking the author of PyODConverter at his web site.
Post a Comment
Post a Comment