我有一个现有的 Excel 文件。我想加载该文件并获取此表中的行数,以便稍后写入此表的下一行并再次保存。我收到以下错误消息:
AttributeError: 'Worksheet' object has no attribute 'nrows'
但显然这种方法是存在的,因为每个人都在用它来计数。
我写的代码看起来像这样:
def write_xls_result(test_case):
testCase = re.sub("/", "_", test_case)
automation_report = os.path.expanduser("~/Library/pathtofile/UITests.xctest/Contents/Resources/Automation_Result.xls")
if os.path.isfile(automation_report):
w = copy(open_workbook(automation_report))
copy_sheet = w.get_sheet(0)
col_width = 256 * 30
try:
for i in itertools.count():
copy_sheet.col(i).width = col_width
except ValueError:
pass
for row in range(copy_sheet.nrows):
print '{} {}'.format("Row COUNT",copy_sheet.nrows)
row_index = 10
copy_sheet.write(row_index,0, testCase)
w.save('Automation_Result.xls')
row_index += 1
print '{} {}'.format("RRRROOOOWWWWW",row_index)
else:
所以我也尝试了一种不同的方法:
def write_xls_result(test_case):
testCase = re.sub("/", "_", test_case)
automation_report = os.path.expanduser("~/Library/pathtofile/UITests.xctest/Contents/Resources/Automation_Result.xls")
if os.path.isfile(automation_report):
workbook = xlrd.open_workbook(automation_report)
result_sheet = workbook.get_sheet(0)
rowcount = result_sheet.nrows
print '{} {}'.format("Row COUNT",rowcount)
col_width = 256 * 30
try:
for i in itertools.count():
result_sheet.col(i).width = col_width
except ValueError:
pass
row_index = 10
result_sheet.write(row_index,0, testCase)
workbook.save('Automation_Result.xls')
row_index += 1
print '{} {}'.format("RRRROOOOWWWWW",row_index)
else:
我收到此错误:
raise XLRDError("Can't load sheets after releasing resources.")
xlrd.biffh.XLRDError: Can't load sheets after releasing resources.
我对Python还是个新手,也许我只是做错了什么。一些帮助或提示会很好。
谢谢