python : Get Active Sheet in xlrd? and help for reading and validating excel file in Python

痞子三分冷 提交于 2019-12-04 18:16:18

Welcome to Stack Overflow.

I have been working with Excel files in Python for a while now, so I could help you with your question, I think.

openpyxl and xlrd solve different problems, one is for xlsx files (Excel 2007+), where the other one is for xls files (Excel 1997-2003), respectively.

Xenon said in his answer that Excel doesn't recognize the concept of an active sheet, which is not totally true. If you open an Excel document, go to some other sheet (that isn't the first one) and save and close the document, the next time you open it, Excel will open the document on the last sheet you were on.

However, xlrd does not support this kind of workflow, i.e. asking for the active sheet. If you know the sheet name, then you could use the method sheet_by_name, or if you know the sheet index, you could use the method sheet_by_index.

I don't know if the xlrd is the best package around, but it is pretty solid, and I have had nary a problem using it.

The example given could be solved by first iterating through the first row and keeping a dictionary of which column a header is. Then storing all the values in the ID column in a list and comparing the length of that list with the length of a set created from that list, i.e. len(values) == len(set(values)). Following that, you could iterate through the column with header of Country and check each value if it is in a dictionary you previously made with all the valid counties.

I hope this answer suits your needs.

Summary: Stick with xlrd because is mature enough.

The "active sheet" here seems you're referring to the last sheet selected when the workbook was saved/closed. You can get this sheet via sheet_visible value.

https://github.com/python-excel/xlrd/blob/master/xlrd/sheet.py#L33

import xlrd
xl = xlrd.open_workbook("example.xls")
for sht in xl.sheets():
    # sht.sheet_visible value of 1 is "active sheet"
    print(sht.name, sht.sheet_selected, sht.sheet_visible)

Usually only one sheet is selected at a time, so it may look like sheet_visible and sheet_selected are the same, but multiple sheets can be selected at a time (ctrl+click multiple sheet tabs, for example).

Another reason this may seem confusing is because Excel uses "visible" in terms of hidden/visible sheets. In xlrd, this is instead sheet.visibility (see https://stackoverflow.com/a/44583134/4258124)

You can see all worksheets in a given workbook with the sheet_names() function. Excel has no concept of an "active sheet", but if my assumption that you are referring to the first sheet is correct, you can get the first element of sheet_names() to get the "active sheet."

With regards to your second question, it's not easy to say that a package is better than another package objectively. However, xlrd is widely used, and the most popular Python library for what it does.

I would recommend sticking with it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!