Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openpyxl - How to read only one column from Excel file in Python?

Tags:

I want to pull only column A from my spreadsheet. I have the below code, but it pulls from all columns.

from openpyxl import Workbook, load_workbook  wb=load_workbook("/home/ilissa/Documents/AnacondaFiles/AZ_Palmetto_MUSC_searchterms.xlsx", use_iterators=True) sheet_ranges=wb['PrivAlert Terms']  for row in sheet_ranges.iter_rows(row_offset=1):      for cell in row:         print(cell.value) 
like image 943
lelarider Avatar asked Jan 12 '16 21:01

lelarider


People also ask

What does the openpyxl load_workbook () function return?

The openpyxl. load_workbook() function takes in the filename and returns a value of the workbook data type. This Workbook object represents the Excel file, a bit like how a File object represents an opened text file.


2 Answers

this is an alternative to previous answers in case you whish read one or more columns using openpyxl

import openpyxl  wb = openpyxl.load_workbook('origin.xlsx') first_sheet = wb.get_sheet_names()[0] worksheet = wb.get_sheet_by_name(first_sheet)  #here you iterate over the rows in the specific column for row in range(2,worksheet.max_row+1):       for column in "ADEF":  #Here you can add or reduce the columns         cell_name = "{}{}".format(column, row)         worksheet[cell_name].value # the value of the specific cell         ... your tasks...  

I hope that this be useful.

like image 54
ZLNK Avatar answered Sep 21 '22 01:09

ZLNK


Using openpyxl

from openpyxl import load_workbook # The source xlsx file is named as source.xlsx wb=load_workbook("source.xlsx")  ws = wb.active first_column = ws['A']  # Print the contents for x in xrange(len(first_column)):      print(first_column[x].value)  
like image 36
Harilal Remesan Avatar answered Sep 19 '22 01:09

Harilal Remesan