You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用numbers-parser 3.9.5读取Numbers单元格返回EmptyCell问题求助

问题描述

使用numbers-parser 3.9.5读取.numbers文件时,执行以下代码:

from numbers_parser import Document
doc = Document("demo.numbers")
sheets = doc.sheets
tables = sheets[0].tables
data = tables["Table 1"].rows()
print("Cell A1 contains", data[0][0])
print("Cell B2 contains", data[1][1])

得到输出:

Cell A1 contains <numbers_parser.cell.EmptyCell object at 0x10d38eb00>
Cell B2 contains <numbers_parser.cell.EmptyCell object at 0x10d38ec50>

实际表格情况:A1单元格为空,B1单元格值为1,B2单元格为空。

原因分析
  1. 代码索引与需求不匹配:你实际想获取的是B1的值,但代码中写的data[1][1]对应B2单元格,而B2本身为空,所以返回EmptyCell是符合实际的。
  2. rows()方法返回表格所有行的单元格列表,空单元格会以EmptyCell对象形式返回,A1本身为空,因此data[0][0]返回空对象是正常表现。
解决方法

方法1:修正行/列索引

如果继续使用rows()方法,要获取B1的值,对应索引为data[0][1](行、列索引均从0开始):

from numbers_parser import Document
doc = Document("demo.numbers")
sheets = doc.sheets
tables = sheets[0].tables
data = tables["Table 1"].rows()
# A1为空,输出EmptyCell是正常结果
print("Cell A1 contains", data[0][0])
# 获取B1的值
print("Cell B1 contains", data[0][1])

方法2:使用cell()方法直接定位(更推荐)

numbers-parser提供cell()方法,可直接通过单元格地址(如"B1")获取值,避免索引混淆:

from numbers_parser import Document
doc = Document("demo.numbers")
sheet = doc.sheets[0]
table = sheet.tables["Table 1"]
# 判断并输出A1单元格内容
a1_cell = table.cell("A1")
print("Cell A1 contains", a1_cell.value if not a1_cell.empty else "Empty")
# 获取B1单元格值
b1_cell = table.cell("B1")
print("Cell B1 contains", b1_cell.value)

通过empty属性可判断单元格是否为空,value属性可获取单元格实际值。

内容的提问来源于stack exchange,提问作者Kiril

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:13:18