如何从 OpenOffice Calc .ods 文件中读取单元格的值?

发布于 2024-08-30 10:10:44 字数 130 浏览 3 评论 0 原文

我已经能够使用 xlrd 使用列号和行号作为输入来读取 Excel 单元格值。现在我需要访问一些以 .ods 格式保存的电子表格中的相同单元格值。

例如,我如何使用 Python 读取 .ods 文件中单元格 E10 中存储的值?

I have been able to read an Excel cell value with xlrd using column and row numbers as inputs. Now I need to access the same cell values in some spreadsheets that were saved in .ods format.

So for example, how would I read with Python the value stored in cell E10 in an .ods file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2024-09-06 10:10:44

通过 XML 破解应该不会太难……但也有一些复杂的情况。仅举一个例子:OOo 以他们的智慧决定不显式地写入单元格地址。没有像 address="E10"column="E" 这样的单元格属性;你需要计算行和列。

五个连续的空单元格由
表示

number-colums-repeated 属性默认为“1”,也适用到非空单元格。

当你合并单元格时,情况会变得更糟;您将获得一个与 table-cell 标记 90% 相同的 covered-table-cell 标记,以及属性 number-columns-spannednumber-rows-spanned 需要计算为列和行计数。

table:table-row 标签可能具有 number-rows-repeated 属性。这可用于重复整个非空行的内容,但最常见于有超过 1 个连续空行时。

因此,即使您对“适用于我的数据”方法感到满意,但这也不是微不足道的。

您可能想查看ODFpy。请注意第二句话:“”“与其他更方便的 API 不同,这个 API 本质上是 XML 格式之上的一个抽象层。”“” 有一个 ODF 到 HTML 脚本(如果它是为 ODS 编写的)对于ODT)可能会被黑客攻击以获得您想要的东西。

如果您更喜欢“适用于几乎每个人的数据并且受支持并且具有您熟悉的界面”的方法,您可能需要等到该功能放入 xlrd 中...但是这个不会很快发生。

Hacking your way through the XML shouldn't be too hard ... but there are complications. Just one example: OOo in their wisdom decided not to write the cell address explicitly. There is no cell attribute like address="E10" or column="E"; you need to count rows and columns.

Five consecutive empty cells are represented by
<table:table-cell table:number-columns-repeated="5" />

The number-colums-repeated attribute defaults to "1" and also applies to non-empty cells.

It gets worse when you have merged cells; you get a covered-table-cell tag which is 90% the same as the table-cell tag, and attributes number-columns-spanned and number-rows-spanned need to be figured into column and row counting.

A table:table-row tag may have a number-rows-repeated attribute. This can be used to repeat the contents of a whole non-empty row, but is most often seen when there are more than 1 consecutive empty rows.

So, even if you would be satisfied with a "works on my data" approach, it's not trivial.

You may like to look at ODFpy. Note the second sentence: """Unlike other more convenient APIs, this one is essentially an abstraction layer just above the XML format.""" There is an ODF-to-HTML script which (if it is written for ODS as well as for ODT) may be hackable to get what you want.

If you prefer a "works on almost everybody's data and is supported and has an interface that you're familiar with" approach, you may need to wait until the functionality is put into xlrd ... but this isn't going to happen soon.

风启觞 2024-09-06 10:10:44

从我尝试过的库中 ezodf 是有效的。

from ezodf import opendoc, Sheet
doc = opendoc('test.ods')
for sheet in doc.sheets:
   print sheet.name
   cell = sheet['E10']
   print cell.value
   print cell.value_type

pyexcel-ods 崩溃,odfpy 崩溃了,此外它的文档要么丢失,要么很糟糕。

鉴于所谓的工作库在我测试的第一个文件上死亡,我宁愿避免编写自己的处理,因为迟早它会崩溃,或者更糟糕的是在一些更奇怪的情况下默默地失败。

编辑:情况变得更糟。 ezodf 可能静默返回虚假数据

From libraries that I tried ezodf was the one that worked.

from ezodf import opendoc, Sheet
doc = opendoc('test.ods')
for sheet in doc.sheets:
   print sheet.name
   cell = sheet['E10']
   print cell.value
   print cell.value_type

pyexcel-ods crashed, odfpy crashed and in addition its documentation is either missing or horrible.

Given that supposedly working libraries died on the first file that I tested I would prefer to avoid writing my own processing as sooner or later it would either crash or what worse fail silently on some weirder situation.

EDIT: It gets worse. ezodf may silently return bogus data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文