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)
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With