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)
By using openpyxl library and Python's list comprehensions concept:
It is pretty amazing approach and worth a try
Use
ws.get_squared_range()
to control precisely the range of cells, such as a single column, that is returned.Using openpyxl
I would suggest using the pandas library.
If you don't feel comfortable in pandas, or for whatever reason need to work with openpyxl, the error in your code is that you aren't selecting only the first column. You explicitly call for each cell in each row. If you only want the first column, then only get the first column in each row.
Using ZLNK's excellent response, I created this function that uses list comprehension to achieve the same result in a single line:
You can then call it by passing a worksheet, a row to begin on and the first letter of any column you want to return:
To return column A and column B, the call changes to this:
this is an alternative to previous answers in case you whish read one or more columns using openpyxl
I hope that this be useful.