LIBREOFFICE_DOC_FAMILIES = [
"TextDocument",
"WebDocument",
"Spreadsheet",
"Presentation",
"Graphics"
] LIBREOFFICE_IMPORT_TYPES = {
"docx": {
"FilterName": "MS Word 2007 XML"
},
"pdf": {
"FilterName": "PDF - Portable Document Format"
},
"jpg": {
"FilterName": "JPEG - Joint Photographic Experts Group"
},
"html": {
"FilterName": "HTML Document"
},
"odp": {
"FilterName": "OpenDocument Presentation (Flat XML)"
},
"pptx": {
"FilterName": "Microsoft PowerPoint 2007 XML"
}
} LIBREOFFICE_EXPORT_TYPES = {
"pdf": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "writer_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[1]: {"FilterName": "writer_web_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[2]: {"FilterName": "calc_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_pdf_Export"}
},
"jpg": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_jpg_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_jpg_Export"}
},
"html": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "HTML (StarWriter)"},
LIBREOFFICE_DOC_FAMILIES[1]: {"FilterName": "HTML"},
LIBREOFFICE_DOC_FAMILIES[2]: {"FilterName": "HTML (StarCalc)"},
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_html_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_html_Export"}
},
"docx": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "MS Word 2007 XML"}
},
"odp": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress8"}
},
"pptx": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "Impress MS PowerPoint 2007 XML"}
}
}

转:

convert_test

#!/usr/bin/env python3
"""
VIEW COMPLETE CODE AT
=====================
* https://github.com/six519/libreoffice_convert
THANKS
======
* Thanks to: Mirko Nasato for his PyODConverter http://www.artofsolving.com/opensource/pyodconverter
TESTED USING
============
* Fedora release 20 (Heisenbug)
* Python 3.3.2
INSTALL DEPENDENCIES
====================
* yum install libreoffice-sdk
""" import uno
import subprocess
import time
import os from com.sun.star.beans import PropertyValue LIBREOFFICE_DEFAULT_PORT = 6519
LIBREOFFICE_DEFAULT_HOST = "localhost" LIBREOFFICE_DOC_FAMILIES = [
"TextDocument",
"WebDocument",
"Spreadsheet",
"Presentation",
"Graphics"
] LIBREOFFICE_IMPORT_TYPES = {
"docx": {
"FilterName": "MS Word 2007 XML"
},
"pdf": {
"FilterName": "PDF - Portable Document Format"
},
"jpg": {
"FilterName": "JPEG - Joint Photographic Experts Group"
},
"html": {
"FilterName": "HTML Document"
},
"odp": {
"FilterName": "OpenDocument Presentation (Flat XML)"
},
"pptx": {
"FilterName": "Microsoft PowerPoint 2007 XML"
}
} LIBREOFFICE_EXPORT_TYPES = {
"pdf": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "writer_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[1]: {"FilterName": "writer_web_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[2]: {"FilterName": "calc_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_pdf_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_pdf_Export"}
},
"jpg": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_jpg_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_jpg_Export"}
},
"html": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "HTML (StarWriter)"},
LIBREOFFICE_DOC_FAMILIES[1]: {"FilterName": "HTML"},
LIBREOFFICE_DOC_FAMILIES[2]: {"FilterName": "HTML (StarCalc)"},
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress_html_Export"},
LIBREOFFICE_DOC_FAMILIES[4]: {"FilterName": "draw_html_Export"}
},
"docx": {
LIBREOFFICE_DOC_FAMILIES[0]: {"FilterName": "MS Word 2007 XML"}
},
"odp": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "impress8"}
},
"pptx": {
LIBREOFFICE_DOC_FAMILIES[3]: {"FilterName": "Impress MS PowerPoint 2007 XML"}
}
} class PythonLibreOffice(object): def __init__(self, host=LIBREOFFICE_DEFAULT_HOST, port=LIBREOFFICE_DEFAULT_PORT):
self.host = host
self.port = port
self.local_context = uno.getComponentContext()
self.resolver = self.local_context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", self.local_context)
self.connectionString = "socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (LIBREOFFICE_DEFAULT_HOST, LIBREOFFICE_DEFAULT_PORT)
self.context = None
self.desktop = None
self.runUnoProcess()
self.__lastErrorMessage = "" try:
self.context = self.resolver.resolve("uno:%s" % self.connectionString)
self.desktop = self.context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.context)
except Exception as e:
self.__lastErrorMessage = str(e) @property
def lastError(self): return self.__lastErrorMessage def terminateProcess(self): try:
if self.desktop:
self.desktop.terminate()
except Exception as e:
self.__lastErrorMessage = str(e)
return False return True def convertFile(self, outputFormat, inputFilename): if self.desktop: tOldFileName = os.path.splitext(inputFilename)
outputFilename = "%s.%s" % (tOldFileName[0], outputFormat)
inputFormat = tOldFileName[1].replace(".","")
inputUrl = uno.systemPathToFileUrl(os.path.abspath(inputFilename))
outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFilename)) if inputFormat in LIBREOFFICE_IMPORT_TYPES:
inputProperties = {
"Hidden": True
} inputProperties.update(LIBREOFFICE_IMPORT_TYPES[inputFormat]) doc = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self.propertyTuple(inputProperties)) try:
doc.refresh()
except:
pass docFamily = self.getDocumentFamily(doc)
if docFamily:
try:
outputProperties = LIBREOFFICE_EXPORT_TYPES[outputFormat][docFamily]
doc.storeToURL(outputUrl, self.propertyTuple(outputProperties))
doc.close(True) return True
except Exception as e:
self.__lastErrorMessage = str(e) self.terminateProcess() return False def propertyTuple(self, propDict):
properties = []
for k,v in propDict.items():
property = PropertyValue()
property.Name = k
property.Value = v
properties.append(property) return tuple(properties) def getDocumentFamily(self, doc):
try:
if doc.supportsService("com.sun.star.text.GenericTextDocument"):
return LIBREOFFICE_DOC_FAMILIES[0]
if doc.supportsService("com.sun.star.text.WebDocument"):
return LIBREOFFICE_DOC_FAMILIES[1]
if doc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
return LIBREOFFICE_DOC_FAMILIES[2]
if doc.supportsService("com.sun.star.presentation.PresentationDocument"):
return LIBREOFFICE_DOC_FAMILIES[3]
if doc.supportsService("com.sun.star.drawing.DrawingDocument"):
return LIBREOFFICE_DOC_FAMILIES[4]
except:
pass return None def runUnoProcess(self):
subprocess.Popen('soffice --headless --norestore --accept="%s"' % self.connectionString, shell=True, stdin=None, stdout=None, stderr=None)
time.sleep(3) if __name__ == "__main__": test_libreoffice = PythonLibreOffice() #convert MS Word Document file (docx) to PDF
test_libreoffice.convertFile("pdf", "document.docx")

Libreoffice 各类文件转换的filtername的更多相关文章

  1. C# 将多个office文件转换及合并为一个PDF文件

    PDF文件介绍 PDF(Portable Document Format )文件源于20世纪90年代初期,如今早已成为了一种最流行的的文件格式之一.因为PDF文件有很多优点: 支持跨平台和跨设备共享 ...

  2. mpp文件转换成jpg图片,可以用pdf文件做中转站

    用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了

  3. php将文件转换成二进制输出[转]

    header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fope ...

  4. ocx文件转换成C#程序引用的DLL

    将ocx文件转换成C#程序引用的DLL文件的办法  将ocx文件转换成C#程序引用的DLL文件的办法,需要的朋友可以参考一下  1.打开VS2008或VS2010命令提示符(此例用VS2008) 将o ...

  5. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...

  6. Python:将utf-8格式的文件转换成gbk格式的文件

    需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: def ReadFile(filePath,encoding="utf-8"): with codecs.ope ...

  7. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

  8. Marvel – 将图像和源文件转换成互动,共享的原型

    Marvel 是一款非常简单的工具,将图像和设计源文件转换成互动,共享的原型,无需任何编码.原型可以通过点击几下鼠标就创建出来,能工作在任何设备上的浏览器,包括移动设备,台式机.Marvel 的一个特 ...

  9. 文件转换神器Pandoc使用

    最近记录笔记,改用Markdown格式.但有时需要分享下笔记,对于不懂markdown格式的同学来说阅读感觉不是那么友好.因此就一直在寻找一款文件转换的软件,之前因为用markdownpad来编写,可 ...

随机推荐

  1. 认识Fiddler

    一.Fiddler界面介绍.(注:下图中的功能区面板显示的是“Inspectors”的选项卡界面) 二.工具栏介绍. 1.气泡:备注.添加之后在会话栏的Comment列中显示备注内容. 2.Repla ...

  2. 蓝桥杯 跳蚱蜢 (bfs)

    转载自:https://blog.csdn.net/wayway0554/article/details/79715658 本题的解题关键就在于将蚱蜢在跳转换为盘子在跳. 当使用string当做每一个 ...

  3. logging 日志

    1. 四步: import logging #初始化 logger = logging.getLogger("log_name") #设置级别 logger.setLevel(lo ...

  4. hdu5745(dp+bitset)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5745 C++ bitset的用法:https://www.cnblogs.com/magisk/p/880 ...

  5. 2200: [Usaco2011 Jan]道路和航线 (拓扑排序+dijstra)

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  6. Xamarin Essentials教程实现数据的传输功能实例

    Xamarin Essentials教程实现数据的传输功能实例 [示例1-1]以下将实现数据的传输功能.代码如下: public async Task ShareUri(string text, st ...

  7. XamarinAndroid组件教程RecylerView适配器设置动画示例

    XamarinAndroid组件教程RecylerView适配器设置动画示例 [示例1-3]下面将在RecylerView的子元素进行滚动时,使用适配器动画.具体的操作步骤如下: (1)创建一个名为R ...

  8. vuex那些事儿

    vuex适用于大型单页应用.每一个Vuex应用的核心就是store(仓库),store中储存大量状态,Vuex的状态存储是响应式的.vuex使用单一状态树,一个对象包含了全部的应用层级状态,每个应用仅 ...

  9. python基础一 ------利用生成器生成一个可迭代对象

    #利用生成器生成一个可迭代对象#需求:生成可迭代对象,输出指定范围内的素数,利用生成器产生一个可迭代对象#生成器:本身是可迭代的,只是 yield 好比return返回,yield返回后函数冻结状态, ...

  10. shell脚本8--录制终端会话

    准备: script -t 2> timing.log -a output.session type commands; ... . .. exit 回放: scriptreplay timin ...