Libreoffice 各类文件转换的filtername
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的更多相关文章
- C# 将多个office文件转换及合并为一个PDF文件
PDF文件介绍 PDF(Portable Document Format )文件源于20世纪90年代初期,如今早已成为了一种最流行的的文件格式之一.因为PDF文件有很多优点: 支持跨平台和跨设备共享 ...
- mpp文件转换成jpg图片,可以用pdf文件做中转站
用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了
- php将文件转换成二进制输出[转]
header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fope ...
- ocx文件转换成C#程序引用的DLL
将ocx文件转换成C#程序引用的DLL文件的办法 将ocx文件转换成C#程序引用的DLL文件的办法,需要的朋友可以参考一下 1.打开VS2008或VS2010命令提示符(此例用VS2008) 将o ...
- nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件
目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...
- Python:将utf-8格式的文件转换成gbk格式的文件
需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: def ReadFile(filePath,encoding="utf-8"): with codecs.ope ...
- 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件
PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...
- Marvel – 将图像和源文件转换成互动,共享的原型
Marvel 是一款非常简单的工具,将图像和设计源文件转换成互动,共享的原型,无需任何编码.原型可以通过点击几下鼠标就创建出来,能工作在任何设备上的浏览器,包括移动设备,台式机.Marvel 的一个特 ...
- 文件转换神器Pandoc使用
最近记录笔记,改用Markdown格式.但有时需要分享下笔记,对于不懂markdown格式的同学来说阅读感觉不是那么友好.因此就一直在寻找一款文件转换的软件,之前因为用markdownpad来编写,可 ...
随机推荐
- pip软件包安装 + Anaconda软件库安装 教程
PIP软件包安装(适用于Ubuntu和Windows10): 下载pip的安装包,官网链接如下:https://pypi.python.org/pypi/pip 我选择了Source源的安装方式,单击 ...
- git checkout 撤销多个文件,撤销整个文件夹
git checkout 撤销多个文件,撤销整个文件夹 git checkout <folder-name>/ git checkout -- <folder-name> 这样 ...
- phpstorm自定义代码片段
右上角file--settings--Editor--Live Templates---+
- HDU-1013的解题报告
题目来源:hdu-1013 题意:正整数的数字根是通过求整数的整数而求出的.如果结果值是一个位数,那么这个数字就是数字根.如果结果值包含两个或多个数字,则将这些数字相加并重复该过程.只要需要获得一个数 ...
- TF:TF下CNN实现mnist数据集预测 96%采用placeholder用法+2层C及其max_pool法+隐藏层dropout法+输出层softmax法+目标函数cross_entropy法+AdamOptimizer算法
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # number 1 to 10 ...
- 从小白到区块链工程师:第一阶段:Go语言的控制台输入和输出(3)
六,Print系列的函数输出 1:Println 打印换行.Print控制台打印,lnline 一行,打印数据后自动换一行显示.下面显示在控制台打印出不同的类型. 打印输出结果后,会自动换一行.打印结 ...
- 利用Fiddler拦截接口请求并篡改数据
近期在测试一个下单的项目,出于安全角度考虑,测试了一个场景,那就是利用工具对接口进行拦截并篡改数据.将接口一拦截并篡改数据后,发现收货满满.开发默默接受了我的建议,并对代码进行了修改. 对于fiddl ...
- VMware5.5-存储
存储 存储类型 VMFS(vmvare公司提供) NFS 本地存储 添加主机硬盘 扩展现有的磁盘或者添加新的硬盘 添加完成后点击全部重新扫描 添加存储器. 网络存储 网络存储的运用大大提高了虚机话的便 ...
- 2017-9-17-MDIO信号线串联小电阻作用【转】
今天做集成测试的时候被领导说测到的MDIO信号过冲较大(正反向过冲都很大),容易损坏接口或阻容,万一那个电容耐压值不够就挂了. 我原本是不屑的,私以为MDIO.IIC.SPI等只要抓到的波形不影响判决 ...
- nodejs,mongodb不同时区问题
问题:不同国家,使用不同时区,而服务器代码却在国内,跨时区日期不同,根据日期查询,查询不到数据了 1.mongodb存储的new Date()是UTC时间,也就是0时区的时间,世界标准时间 2.参考m ...