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
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.
I would suggest using the pandas library.
import pandas as pd
dataFrame = pd.read_excel("/home/ilissa/Documents/AnacondaFiles/AZ_Palmetto_MUSC_searchterms.xlsx", sheetname = "PrivAlert Terms", parse_cols = 0)
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.
for row in sheet_ranges.iter_rows(row_offset=1):
print(row[0].value)
By using openpyxl library and Python's list comprehensions concept:
import openpyxl
book = openpyxl.load_workbook('testfile.xlsx')
user_data = book.get_sheet_by_name(str(sheet_name))
print([str(user_data[x][0].value) for x in range(1,user_data.max_row)])
It is pretty amazing approach and worth a try
In my opinion is much simpler
from openpyxl import Workbook, load_workbook
wb = load_workbook("your excel file")
source = wb["name of the sheet"]
for cell in source['A']:
print(cell.value)
Use ws.get_squared_range()
to control precisely the range of cells, such as a single column, that is returned.
Here is a simple function:
import openpyxl
def return_column_from_excel(file_name, sheet_name, column_num, first_data_row=1):
wb = openpyxl.load_workbook(filename=file_name)
ws = wb.get_sheet_by_name(sheet_name)
min_col, min_row, max_col, max_row = (column_num, first_data_row, column_num, ws.max_row)
return ws.get_squared_range(min_col, min_row, max_col, max_row)