Python、OpenOffice:以编程方式操作电子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2356473/
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-04 00:26:17  来源:igfitidea点击:

Python, OpenOffice: Programmatically Manipulating spreadsheets

pythonexcelopenoffice-calc

提问by Adam Matan

I have an spreadsheet-based automated report that needs to be created daily, with some charts, aggregating functions (e.g. SUM and AVERAGE) and formatted cells (Dates, percentage, etc.).

我有一个需要每天创建的基于电子表格的自动报告,其中包含一些图表、聚合函数(例如 SUM 和 AVERAGE)和格式化的单元格(日期、百分比等)。

I have tried to write these results directly to an Excel file, but Python's xlwtand xlrddon'y support charts and functions.

我曾尝试将这些结果直接写入 Excel 文件,但 Python 的xlwtxlrd不支持图表和函数。

Moreover, trying to open an existing, formatted Excel file and changing some cell's values ended up erasing all charts and functions in the existing file.

此外,尝试打开现有的格式化 Excel 文件并更改某些单元格的值会导致删除现有文件中的所有图表和函数。

Is there a way to write charts and functions to an OpenOffice spreadsheet, or at least change cells in an existing spreadsheet without erasing data? If there is a Pythonic way to do it, I can easily convert the OO file into an Excel file and deliver it.

有没有办法将图表和函数写入 OpenOffice 电子表格,或者至少在不删除数据的情况下更改现有电子表格中的单元格?如果有 Pythonic 的方法,我可以轻松地将 OO 文件转换为 Excel 文件并交付。

采纳答案by Desintegr

You can use PyUNO, a Python library to use UNO API.

您可以使用PyUNO(一个 Python 库)来使用 UNO API。

Hereis a Python example to do some manipulations in a Calc document.

是在 Calc 文档中进行一些操作的 Python 示例。

回答by S.Lott

Are you looking for this: http://ooopy.sourceforge.net/

你在找这个吗:http: //ooopy.sourceforge.net/

Open Office.org API's accessible from Python?

可以从 Python 访问 Open Office.org API 吗?

Or this? http://api.openoffice.org/

或这个? http://api.openoffice.org/

The OpenOffice.org API Project?

OpenOffice.org API 项目?

This may be helpful, also: http://wiki.services.openoffice.org/wiki/Python

这可能也有帮助:http: //wiki.services.openoffice.org/wiki/Python

回答by flowtron

For creating spreadsheets easily: use odslibor for python3: odslib3… even if the project was last updated over six years ago (2013-07-19) it worked straight out of the box and with just one example viewed. Download the package from the repositoryfor the examples.

轻松创建电子表格:使用odslib或 python3:odslib3......即使该项目上次更新是在六年前(2013-07-19),它也可以直接开箱即用,并且只查看了一个示例。从存储库下载示例包。

$ pip install odslib

and then a sample spreadsheet could be created like this:

然后可以像这样创建一个示例电子表格:

#!/usr/bin/python

import sys
import odslib

doc = odslib.ODS()

def writeRow( doc, row, name, power, rating ):
    doc.content.getCell( 0, row).stringValue( name )
    doc.content.getCell( 1, row).stringValue( power )
    doc.content.getCell( 2, row).floatValue( rating )

# the column names
writeRow( doc, 0, "Name", "Power", "Rating" ) 

# some lines of content
writeRow( doc, 1, "Mr. Incredible", "Strength", 0.8 ) 
writeRow( doc, 2, "Elastigirl", "Elasticity", 0.9 ) 
writeRow( doc, 3, "Frozone", "Ice", 0.7 ) 
writeRow( doc, 4, "Syndrome", "n/a", 0.3 ) 
writeRow( doc, 5, "Hyman-Hyman", "All", 1.0 ) 

doc.save("supers.ods")