-4

My excel data looks like this:

    A    B    C
1   RAM  53.44  576
2   MOHAN  74.34  345
3   KISHAN  76.65  285

How can I extract headers contents from spreadsheets?

soheshdoshi
  • 594
  • 3
  • 7
  • 24
  • I edited my solution now it conforms to your file format – CodeSamurai777 Jul 27 '18 at 12:41
  • In Excel and Google Sheets, the column heading or column header is the gray-colored row containing the letters (A, B, C, etc.) used to identify each column in the worksheet. The column header is located above row 1 in the worksheet. What you ask is obviously not what you want. Please refine your question. – BoboDarph Jul 27 '18 at 12:43
  • 1
    You need to conform to some kind of file format, it is not possible to detect if a row contain headers or is it just a row of string data. The solution I posted is correct, it does exactly what you want for the provided sample. If the row placement can change you can store the value for `skiprows` in a cell read it and then pass it to `pd.excel_read()` – CodeSamurai777 Jul 27 '18 at 13:00
  • i have one solution detect any bold format because all headers are in always bold format in my case @mrangry777 – soheshdoshi Jul 27 '18 at 13:14

3 Answers3

9

Your question is really unclear. However you can use pandas library to read file excel format.

import pandas as pd
print(pd.read_excel('file.xlsx').columns)

Call to .columns list the headers of all the columns

Output example:

Index(['id', 'email','server','profession'], dtype='object')

EDIT: Ok now I see you have very odd first to rows which do not comply with pandas data structure. The simplest solution is to skip the first two rows. And then import it into panadas DataFrame

use skiprows to skip first n rows. In your case 2

import pandas as pd
print(pd.read_excel('file.xlsx',skiprows=2).columns)
CodeSamurai777
  • 3,285
  • 2
  • 24
  • 42
  • i need only headers no data see the excel image how i can identify headers in this excel file @mrangry7777 – soheshdoshi Jul 27 '18 at 12:28
  • 1
    call to `columns` gives you the name of the headers it does not fetch any data. My example comes from a file with 4 columns: id,email,server,profession. No data was fetched only the headers – CodeSamurai777 Jul 27 '18 at 12:29
  • @mrangry7777 it's not proper solution suppose headers start from 15th row how you can predict that without skip-rows? – soheshdoshi Jul 27 '18 at 12:54
0

Check out Pandas. It's a lot better than Xlrd and uses less code.

You should be able to get it's index, regardless of whether or not it's a header.

From memory think you have to use df.head() once you've created your dataframe.

Easias
  • 1
  • 4
0
#static code if header at 0,0 position
import xlrd 

loc = ("path of file") 

wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 

# For row 0 and column 0 
sheet.cell_value(0, 0) 

for i in range(sheet.ncols): 
    print(sheet.cell_value(0, i)) 
soheshdoshi
  • 594
  • 3
  • 7
  • 24