简体   繁体   English

Python:打开现有的Excel文件并计算工作表中的行数

[英]Python: open existing Excel file and count rows in sheet

I have an existing Excel file. 我有一个现有的Excel文件。 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:

And I get this Error: 我得到这个错误:

raise XLRDError("Can't load sheets after releasing resources.")
xlrd.biffh.XLRDError: Can't load sheets after releasing resources.

I am still new to python, maybe I am just doing something wrong. 我仍然是python的新手,也许我只是做错了什么。 Some help or hints would be nice. 一些帮助或提示会很好。 thanks 谢谢

Your top code is either run differently, or is missing the xlrd portion of xlrd.open_workbook ... 你的顶部代码是不同的运行,或缺少xlrd的部分xlrd.open_workbook ...

You can get the result_sheet without that error by using: 您可以使用以下命令获取没有该错误的result_sheet

result_sheet = workbook.sheet_by_index(0)

(I get an error trying .get_sheet ) (尝试.get_sheet

What library are you using? 你在用什么图书馆? Just xlrd ? 只是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. 我没有看到列的.width属性(至少在我的示例中,它是类型list ),并且无论如何也不确定您正在使用该部分代码。

Do you always want to write the number of rows found into row 10? 你总是想写下第10行的行数吗? That number never gets indexed in a functional way, and the last line before the else is always going to print 11 . 这个数字永远不会以函数方式编入索引,而else之前的最后一行总是打印11

The second approach is correct except that you should replace: 第二种方法是正确的,除非您应该替换:

w.save('Automation_Result.xls')

with: 有:

workbook.save('Automation_Result.xls')

Since the workbook variable is the reference to the xlrd Workbook you've opened in the code block. 由于workbook变量是您在代码块中打开的xlrd工作簿的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 xlwings 在 Python 中计算 Excel 工作表中的行数 - Count rows in excel sheet in Python with xlwings Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file 打开现有 Excel 文件、清除现有工作表并将数据框写入该工作表的最佳方法 - Best way to open existing excel file, clear an existing sheet and write dataframe to that sheet 想要读取一个文件并使用 python 写入现有的 excel 表 - Want to read an a file and write to existing excel sheet using python 打开现有的 excel 文件并在 Python 中写入新行 - Open an existing excel file and writing a new line in Python 为什么openpyxl不识别我打开的现有excel文件中的工作表名称? - Why doesn't openpyxl recognize the name of a sheet in an existing excel file that I open? 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python 如何在Python中使用XLWT附加到现有的Excel工作表 - How to append to an existing excel sheet with XLWT in Python Python-使用现有格式保存新的Excel工作表 - Python - save new Excel sheet with existing format Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM