January 2019
Beginner
318 pages
8h 23m
English
In this section, we are extracting column names from the Excel sheet. Create a script called extract_column_names.py and write the following content in it:
import xlrdexcel_file = ("/home/student/work/sample.xlsx")book_obj = xlrd.open_workbook(excel_file)excel_sheet = book_obj.sheet_by_index(0)excel_sheet.cell_value(0, 0)for i in range(excel_sheet.ncols): print(excel_sheet.cell_value(0, i))
Run the script and you will get the following output:
student@ubuntu:~/work$ python3 extract_column_names.py
Following is the output:
IdFirst NameLast NameGenderAgeCountry
In the preceding example, we are extracting the column names from the Excel sheet. We fetched the column names using the ncols attribute.
Read now
Unlock full access