通过 python 添加 sheet 到 ods 文件

add a sheet to an ods file via python

我从包 simpleodspy 开始

在我想向 ods 文件添加新的 sheet 之前,它工作正常。

我发现 simpleodspy 只是 odfpy 的一个更好的包装器,可以简化 api。所以我猜他们 scipted 添加了一个 sheet 部分。

所以我试图弄清楚如何使用 odfpy 来完成它。

但我坚持了。

有人知道是否可以将 sheet 添加到 ods 文件吗?

如果是怎么办?

这是添加新 sheet 的示例代码:

from odf.opendocument import load
from odf.table import Table
from odf.table import TableCell
from odf.table import TableRow
from odf.text import P

#loading it
book = load('your.ods')
#create a new sheet in memory
sheet = Table(name="test sheet")
#create a new row
tablerow=TableRow()
#create a new cell
cell=TableCell()
#fill-in a cell
cell.addElement(P(text="hello world"))
#add a cell to the row
tablerow.addElement(cell)
#add the row to the sheet
sheet.addElement(tablerow)
#adding the new to the book
book.spreadsheet.addElement(sheet)
#save it
book.save('your_new.ods')