Converting file formats using OpenOffice

From Nearline Storage
Jump to: navigation, search

Also look at catdoc, which is available for *IX and with cygwin. There's a package for Fedora. Google Desktop search on Linux uses this to extract content for indexing of MS Office files (/opt/google/desktop/bin/extract_msoffice_content.sh).

But then ...

The following stolen from Bob DuCharme at http://www.xml.com/lpt/a/1638

Create a ConvertFile macro in OpenOffice Tools -> Macros -> OrganizeMacros -> OpenOffice.org Basic:

Error creating thumbnail: Unable to save thumbnail to destination
Sub SaveAsPDF( cFile )
   cURL = ConvertToURL( cFile )
   ' Open the document. Just blindly assume that the document 
   ' is of a type that OOo will correctly recognize and open 
   ' without specifying an import filter.
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, Array(MakePropertyValue( "Hidden", True ),))

   cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
   cURL = ConvertToURL( cFile )
   
   ' Save the document using a filter.   
   oDoc.storeToURL( cURL, Array(MakePropertyValue( "FilterName", "writer_pdf_Export" ),)
   
   oDoc.close( True )
End Sub

' Save document as a Microsoft Word file. 
Sub SaveAsDoc( cFile ) 
   ' mostly a copy of SaveAsPDF
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, (Array(MakePropertyValue( "Hidden", True ),))


   cFile = Left( cFile, Len( cFile ) - 4 ) + ".doc"
   cURL = ConvertToURL( cFile )
   
   oDoc.storeToURL( cURL, Array(MakePropertyValue( "FilterName", "MS WinWord 6.0" ),)
   oDoc.close( True )

End Sub

' Save document as a CSV file. 
Sub SaveAsCSV( cFile ) 
   ' mostly a copy of SaveAsPDF
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, (Array(MakePropertyValue( "Hidden", True ),))

   cFile = Left( cFile, Len( cFile ) - 4 ) + ".csv"
   cURL = ConvertToURL( cFile )
   
   oDoc.storeToURL( cURL, Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ),)
   oDoc.close( True )

End Sub


' Save document as an OpenOffice 2 file. 
Sub SaveAsOOO( cFile ) 
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, Array(MakePropertyValue( "Hidden", True ),))

   ' Set output file extension based on lower-case 
   ' version of input extension.
   Select Case LCase(Right(cFile,3))
     Case "ppt"         ' PowerPoint file.
       cFileExt = "odp"
     Case "doc"         ' Word file.
       cFileExt = "odt"
     Case "xls"         ' Excel file.
       cFileExt = "ods"
     Case Else
       cFileExt = "xxx"
    End Select
       
   cFile = Left( cFile, Len( cFile ) - 3 ) + cFileExt
   cURL = ConvertToURL( cFile )
   
   oDoc.storeAsURL( cURL, Array() )
   oDoc.close( True )

End Sub


Function MakePropertyValue( Optional cName As String, Optional uValue ) _
   As com.sun.star.beans.PropertyValue
   Dim oPropertyValue As New com.sun.star.beans.PropertyValue
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function

'  Stolen from DannyB  http://www.oooforum.org/forum/viewtopic.phtml?t=3549
'  Lists all the possible filter names that could be used in a file converter
Function ListAllFilters
   oFF = createUnoService( "com.sun.star.document.FilterFactory" )
   oFilterNames = oFF.getElementNames()
   
   ' Create a Writer doc and save the filter names to it.
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
   oText = oDoc.getText()
   oCursor = oText.createTextCursor()
   oCursor.gotoEnd( False )
   
   ' Print the filter names into a Writer document.
   For i = LBound( oFilterNames ) To UBound( oFilterNames )
      oText.insertString( oCursor, oFilterNames(i), False )
      oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
   Next
End Function

From the command line, in Windows:

"C:\Program Files\OpenOffice.org 2.0\program\soffice" -invisible macro:///Standard.MyConversions.SaveAsCSV(c:\temp\sample.xls)

In Linux:

soffice -invisible "macro:///Standard.MyConversions.SaveAsCSV(/home/dlk/temp/sample.xls)"