2 Questions to ask:
Ques 1:
I just started studying about xlrd for reading excel file in python.
I was wondering if there is a method in xlsrd --> similar to get_active_sheet()
in openpyxl or any other way to get the Active sheet ?
get_active_sheet()
works this in openpyxl
import openpyxl wb = openpyxl.load_workbook('example.xlsx') active_sheet = wb.get_active_sheet()
output : Worksheet "Sheet1"
I had found methods in xlrd
for retrieving the names of sheets, but none of them could tell me the active sheet.
Ques 2:
Is xlrd
the best packaage in python for reading excel files? I also came across this which had info about other python packages(xlsxwriter
xlwt
xlutils
) for reading and writing excel files.
Which of the above all will be best for making an App which reads an Excel File and applies different validations to to different columns
For eg: Column with Header 'ID' should have unique values and A column with Header 'Country' should have valid Countries.
Welcome to
Stack Overflow
.I have been working with
Excel
files inPython
for a while now, so I could help you with your question, I think.openpyxl
andxlrd
solve different problems, one is forxlsx
files (Excel 2007+), where the other one is forxls
files (Excel 1997-2003), respectively.Xenon
said in his answer thatExcel
doesn't recognize the concept of anactive
sheet, which is not totally true. If you open anExcel
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 theactive
sheet. If you know the sheet name, then you could use the methodsheet_by_name
, or if you know the sheet index, you could use the methodsheet_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.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 ofsheet_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.
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
Usually only one sheet is selected at a time, so it may look like
sheet_visible
andsheet_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)