Python:打开现有的Excel文件并计算工作表中的行 [英] Python: open existing Excel file and count rows in sheet
问题描述
我有一个现有的Excel文件。我想加载该表并获取该表中的行数,以便稍后在该表的下一行中进行写入并再次保存。我收到以下错误消息:
I have an existing Excel file. I want to load that one and get the count of rows in this sheet, to later write in the next row of this sheet and save it again. I get following error messages:
AttributeError: 'Worksheet' object has no attribute 'nrows'
但显然存在这种方法,因为每个人都在使用它来获取计数。
我编写的代码如下:
But clearly this method exists, coz everyone is using it to get the count. The Code I wrote looks like this:
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:
所以我也尝试了另一种方法:
So I tried a different approach as well:
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还是陌生的,也许我做错了什么。一些帮助或提示会很好。
谢谢
I am still new to python, maybe I am just doing something wrong. Some help or hints would be nice. thanks
推荐答案
您的顶级代码运行方式不同,或者缺少 xlrd
部分 xlrd.open_workbook
...
Your top code is either run differently, or is missing the xlrd
portion of xlrd.open_workbook
...
您可以获得 result_sheet
使用以下方法不会出现该错误:
You can get the result_sheet
without that error by using:
result_sheet = workbook.sheet_by_index(0)
(尝试 .get_sheet
时出现错误)
您正在使用哪个库?只是 xlrd
?我没有看到列的 .width
属性(至少在我的示例案例中,它是 list
类型),并且不确定该代码的那部分在做什么。
What library are you using? Just xlrd
? I don't see a .width
property of a column (at least in my example case, it is type list
), and not sure what you are doing with that part of the code anyway.
您是否总是想将找到的行数写入第10行?该数字永远不会以功能性方式建立索引,而其他位置之前的最后一行将始终打印 11
。
Do you always want to write the number of rows found into row 10? That number never gets indexed in a functional way, and the last line before the else is always going to print 11
.
这篇关于Python:打开现有的Excel文件并计算工作表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!