python ODFPy 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/949171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:09:14  来源:igfitidea点击:

ODFPy documentation

pythonopenoffice.orgodfodfpy

提问by

I need to manipulate the ODF file format (open document format, the open office's internal format), and I need to do it in Python.

我需要操作ODF文件格式(开放文档格式,开放办公的内部格式),我需要用Python来做。

It seem's ODFPy is a wonderful library for this purpose. Unfortunately the official documentation is very poor, almost unuseful. I can't find almost anything online - maybe it is not so popular?

似乎 ODFPy 是用于此目的的绝佳库。不幸的是官方文档很差,几乎没有用。我在网上几乎找不到任何东西 - 也许它不是那么受欢迎?

Is there anyone who can point me at some some information or better documentation?

有没有人可以指点我一些信息或更好的文档?

回答by Nick Bastin

The documentation is unfortunately horrible, and the generated Python wrapper is lousily documented in code, providing lots of functions whose argument lists look like func(*args).

不幸的是,文档很糟糕,生成的 Python 包装器在代码中记录得很糟糕,提供了许多参数列表看起来像 func(*args) 的函数。

The reference manual isactually useful, but not when you're starting out - it doesn't provide any context of how to use these functions. I would suggest starting with the tutorialand all the examples. Even though they may have nothingto do with your use case, they will help you get the feel of how the package works. After you've gotten used to the way the package is structured, you can often make sense of the documentation by combining the API doc with the information in the OpenDocument Essentialsbook.

参考手册真正有用的,但是当你开始不是-它不提供如何使用这些功能的任何背景。我建议从教程和所有示例开始。尽管它们可能与您的用例无关,但它们将帮助您了解包的工作原理。在您习惯了包的结构方式后,您通常可以通过将 API 文档与OpenDocument Essentials一书中的信息相结合来理解文档。

(The relationship is somewhat tenuous at best, but you can often intuit method and attribute values from it. When working with the spreadsheet, for example, the handy list of office:value-type data in the book provided the necessary constants for building proper TableCell(valuetype=...) instances)

(这种关系充其量多少有些脆弱,但您通常可以从中直观地了解方法和属性值。例如,在使用电子表格时,书中的 office:value 类型数据的方便列表提供了构建正确的必要常量TableCell(valuetype=...) 实例)

Also, making small documents in OpenOffice and then inspecting the xml and comparing it to the XML generated from ODFPy greatly helps you debug where you might have gone wrong.

此外,在 OpenOffice 中制作小文档,然后检查 xml 并将其与从 ODFPy 生成的 XML 进行比较,极大地帮助您调试可能出错的地方。

回答by mrmagooey

There's a good example of odfpy usage at http://mashupguide.net/1.0/html/ch17s04.xhtml

http://mashupguide.net/1.0/html/ch17s04.xhtml 上有一个很好的 odfpy 用法示例

回答by user1928764

I found more documentation (the web site has been reorganized in the past few years) in api-for-odfpy.odt.

我在api-for-odfpy.odt 中找到了更多文档(该网站在过去几年中进行了重组)。

回答by Anton

It's outdated, a bit, but could help someone. I have found only one way to work with ODFPY:

它有点过时了,但可以帮助某人。我只找到了一种使用 ODFPY 的方法:

  1. generate your ODF document (i.e. f1.ods)
  2. make copy of it and edit in LibreOffice/OpenOffice or other (i.e. f2.odf)
  3. change both files to f1.zip and f2.zip
  4. extract both files.
  1. 生成您的 ODF 文档(即 f1.ods)
  2. 复制它并在 LibreOffice/OpenOffice 或其他(即 f2.odf)中编辑
  3. 将两个文件更改为 f1.zip 和 f2.zip
  4. 提取这两个文件。

main formatting and data is located in "content.xml" and "styles.xml"

主要格式和数据位于“content.xml”和“styles.xml”

  1. compare both formatting
  2. make changes in python script
  3. iterate 1-7 until you have sufficient result :D:D
  1. 比较两种格式
  2. 在python脚本中进行更改
  3. 迭代 1-7 直到你有足够的结果:D:D

Here is some date-time format example, I have made that way:

这是一些日期时间格式示例,我是这样做的:

from odf.opendocument import OpenDocumentSpreadsheet
from odf.style import Style, TableCellProperties
from odf.number import DateStyle, Text, Year, Month, Day, Hours, Minutes, Seconds
from odf.text import P
from odf.table import Table, TableRow, TableCell

# Generate document object
doc = OpenDocumentSpreadsheet()
table = Table(name="Exported data")
#create custom format in styles.xml
date_style = DateStyle(name="date-style1") #, language="lv", country="LV")
date_style.addElement(Year(style="long"))
date_style.addElement(Text(text=u"-"))
date_style.addElement(Month(style="long"))
date_style.addElement(Text(text=u"-"))
date_style.addElement(Day(style="long"))
date_style.addElement(Text(text=u" "))
date_style.addElement(Hours(style="long"))
date_style.addElement(Text(text=u":"))
date_style.addElement(Minutes(style="long"))
date_style.addElement(Text(text=u":"))
date_style.addElement(Seconds(style="long", decimalplaces="3"))
doc.styles.addElement(date_style)
#link to generated style from content.xml
ds = Style(name="ds1", datastylename="date-style1",parentstylename="Default", family="table-cell")
doc.automaticstyles.addElement(ds)

#create simple cell
tr = TableRow()
tc = TableCell(valuetype='string')
tc.addElement(P(text = "Date-Time"))
tr.addElement(tc)
table.addElement(tr)

#create cell with custom formatting
lineDT = #some date-time variable
tr = TableRow()
tc = TableCell(valuetype='date',datevalue = lineDT.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3],stylename=ds)
tc.addElement(P(text=lineDT.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]))
tr.addElement(tc)
table.addElement(tr)

#save ods
doc.spreadsheet.addElement(table)
doc.save("test.ods", True)

I have updated code, a bit, because previous version opened wrong on MS product.

我更新了一些代码,因为以前的版本在 MS 产品上打开错误。

回答by yvess

try ezodfthey also have a doc

尝试ezodf他们也有一个文档

回答by nosklo

Okay, here's a quick help:

好的,这是一个快速帮助:

  1. Grab odfpy source code:

    ~$ svn checkout https://svn.forge.osor.eu/svn/odfpy/trunk odfpy
    
  2. Install it:

     ~$ cd odfpy
     ~/odfpy$ python setup.py install
    
  3. Generate the documentation:

    ~/odfpy$ epydoc --pdf odf
    

    I have uploaded the generated documentation here.

  4. Run this simple example program:

    from odf.opendocument import OpenDocumentText
    from odf.text import P    
    textdoc = OpenDocumentText()
    p = P(text="Hello World!")
    textdoc.text.addElement(p)
    textdoc.save("helloworld", True)
    
  5. Read the examples and try to make sense of everything:

    ~/odfpy$ emacs examples/*.py
    
  1. 获取 odfpy 源代码:

    ~$ svn checkout https://svn.forge.osor.eu/svn/odfpy/trunk odfpy
    
  2. 安装它:

     ~$ cd odfpy
     ~/odfpy$ python setup.py install
    
  3. 生成文档:

    ~/odfpy$ epydoc --pdf odf
    

    我已经在这里上传了生成的文档。

  4. 运行这个简单的示例程序:

    from odf.opendocument import OpenDocumentText
    from odf.text import P    
    textdoc = OpenDocumentText()
    p = P(text="Hello World!")
    textdoc.text.addElement(p)
    textdoc.save("helloworld", True)
    
  5. 阅读示例并尝试理解所有内容:

    ~/odfpy$ emacs examples/*.py
    

Hope that helps! Good luck!

希望有帮助!祝你好运!