Linux OpenOffice Convert Office to PDF

Source: Internet
Author: User

1. Save the following code as documentconvert.py

2. Copy the file to the $OPENOFFICE/program ($OPENOFFICE main directory)

3. After entering the program directory, start the OpenOffice service and start the service command as follows:

./soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-nofirststartwizard &

4. Perform the following command for document conversion:

./python documentconverter.py/opt/shanhy/test.doc/opt/shanhy/test.pdf# The first parameter is the doc file to convert, the second parameter is the converted PDF file location

5, the same command can also convert other format files, we look at the following script code research research.


documentconvert.py Script source code is as follows:

# # Pyodconverter (Python OpenDocument Converter) v1.1-2009-11-14## This script converts a document from one office forma T to another by# connecting to an OpenOffice.org instance via Python-uno bridge.## Copyright (C) 2008-2009 Mirko Nasato &L T [email protected]># Licensed under the GNU LGPL v2.1-http://www.gnu.org/licenses/lgpl-2.1.html#-or any later V ersion. #DEFAULT_OPENOFFICE_PORT = 8100import unofrom os.path import Abspath, isfile, Splitextfrom Com.sun.star.beans Import propertyvaluefrom com.sun.star.task import errorcodeioexceptionfrom com.sun.star.connection Import Noconnectexceptionfamily_text = "TEXT" Family_web = "WEB" Family_spreadsheet = "SPREADSHEET" family_presentation = " Presentation "family_drawing =" DRAWING "#---------------------# # Configuration Start # #---------------------# # See http://wiki.services.openoffice.org/wiki/Framework/Article/Filter# most formats is auto-detected;     Only those requiring options is defined Hereimport_filter_map = {"txt": {   "FilterName": "Text (encoded)", "filteroptions": "UTF8"}, "CSV": {"filtername": "Text-txt-csv  (Starcalc) "," Filteroptions ":" 44,34,0 "}}export_filter_map = {" pdf ": {family_text: {" FilterName ": "Writer_pdf_export"}, Family_web: {"filtername": "Writer_web_pdf_export"}, Family_spreadsheet: {"Filter Name ":" Calc_pdf_export "}, Family_presentation: {" filtername ":" Impress_pdf_export "}, Family_drawing: {" FilterName ":" Draw_pdf_export "}}," html ": {family_text: {" filtername ":" HTML (StarWriter) "}, Fami    Ly_spreadsheet: {"filtername": "HTML (Starcalc)"}, Family_presentation: {"filtername": "Impress_html_export"}     }, "Odt": {family_text: {"filtername": "Writer8"}, Family_web: {"filtername": "Writerweb8_writer"} }, "Doc": {family_text: {"filtername": "MS Word"}}, "RTF": {family_text: {"FilterName"  : "Rich Text Format"}  }, "txt": {family_text: {"filtername": "TEXT", "filteroptions": "UTF8"}}, "ODS": {family_spreadsheet: {"filtername": "Calc8"}}, "xls": {family_spreadsheet: {"Filternam             E ":" MS Excel "}}," CSV ": {family_spreadsheet: {" filtername ":" Text-txt-csv (Starcalc) ",     "Filteroptions": "44,34,0"}, "ODP": {family_presentation: {"filtername": "Impress8"} }, "ppt": {family_presentation: {"filtername": "MS PowerPoint"}}, "swf": {Family_drawin G: {"filtername": "Draw_flash_export"}, Family_presentation: {"filtername": "Impress_flash_export"}}}PAGE_S Tyle_override_properties = {family_spreadsheet: {#---scale options:uncomment 1 of the 3---# a) ' Red Uce/enlarge printout ': ' Scaling factor ' "PageScale": +, # b) ' Fit print range (s) to Width/height ': ' Wi DTH in pages ' and ' HeighT in pages ' # "Scaletopagesx": 1, "Scaletopagesy": +, # C) ' Fit print range (s) on number of pages ': ' Fit P Rint Range (s) on number of pages ' # "Scaletopages": 1, "PrintGrid": False}}#-------------------# # Configu Ration End # #-------------------#class documentconversionexception (Exception): Def __init__ (self, message): Self . Message = Message def __str__ (self): return self.messageclass documentconverter:def __init__ (self, port =default_openoffice_port): Localcontext = Uno.getcomponentcontext () resolver = localcontext.servicemanager.c Reateinstancewithcontext ("Com.sun.star.bridge.UnoUrlResolver", localcontext) Try:context = Resolver.res Olve ("UNO:SOCKET,HOST=LOCALHOST,PORT=%S;URP; Staroffice.componentcontext "% port" except Noconnectexception:raise documentconversionexception, "fail Ed to connect to OpenOffice.org on port%s "% Port Self.desktop = context. Servicemanager.createInstancewithcontext ("Com.sun.star.frame.Desktop", context) def convert (self, inputfile, outputFile): Inputurl =        Self._tofileurl (inputfile) Outputurl = Self._tofileurl (outputFile) loadProperties = {"Hidden": True} InputExt = Self._getfileext (inputfile) if Import_filter_map.has_key (inputext): Loadproperties.update (I Mport_filter_map[inputext]) document = Self.desktop.loadComponentFromURL (Inputurl, "_blank", 0, Self._topro Perties (loadProperties)) Try:document.refresh () except Attributeerror:pass FA mily = self._detectfamily (document) self._overridepagestyleproperties (document, family) Outputext =            Self._getfileext (outputFile) storeproperties = self._getstoreproperties (document, Outputext) Try: Document.storetourl (Outputurl, Self._toproperties (storeproperties)) finally:document.close (True) de F _overridepagestylepropErties (self, document, family): if Page_style_override_properties.has_key (family): PROPERTIES = Page_sty Le_override_properties[family] Pagestyles = Document.getstylefamilies (). Getbyname (' Pagestyles ') for StyleName in Pagestyles.getelementnames (): Pagestyle = Pagestyles.getbyname (stylename) for n Ame, value in Properties.items (): Pagestyle.setpropertyvalue (name, value) def _getstoreproperties (SE LF, document, Outputext): Family = self._detectfamily (document) try:propertiesbyfamily = Export_ Filter_map[outputext] except keyerror:raise documentconversionexception, "Unknown output format: '%s '" % Outputext Try:return propertiesbyfamily[family] except keyerror:raise documentconv Ersionexception, "Unsupported Conversion:from '%s ' to '%s '"% (family, Outputext) def _detectfamily (self, document ): If document.suPportsservice ("Com.sun.star.text.WebDocument"): Return Family_web if Document.supportsservice ("Com.sun.s Tar.text.GenericTextDocument "): # must be TextDocument or globaldocument return family_text I F Document.supportsservice ("Com.sun.star.sheet.SpreadsheetDocument"): Return Family_spreadsheet if Docum Ent.supportsservice ("Com.sun.star.presentation.PresentationDocument"): Return family_presentation if doc Ument.supportsservice ("Com.sun.star.drawing.DrawingDocument"): Return family_drawing raise Documentconve        Rsionexception, "Unknown document family:%s"% document DEF _getfileext (self, path): ext = splitext (path) [1] If ext isn't None:return ext[1:].lower () def _tofileurl (self, path): Return Uno.systempathtofi Leurl (Abspath (path)) def _toproperties (self, dict): props = [] for key in Dict:prop = Propert Yvalue () prop. Name = key Prop. Value = Dict[key] Props.append (prop) return tuple (props) If __name__ = "__main__": from sys import AR        GV, exit If Len (argv) < 3:print "Usage:python%s <input-file> <output-file>"% argv[0] Exit (255) if not isfile (Argv[1]): print "No such input file:%s"% argv[1] Exit (1) Try:conv Erter = Documentconverter () Converter.convert (argv[1], argv[2]) except Documentconversionexception, exceptio N:print "error!" + str (Exception) exit (1) except Errorcodeioexception, Exception:print "error! Errorcodeioexception%d "% exception. Errcode exit (1)



Linux OpenOffice Convert Office to PDF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.