ws.cell(1,1)
ws[1,1]
I have a large xlsx file with rows of data that I want to iterate over. I want to insert the data into a SQL database so want to pick out data from individual columns on each row.
The nearest openpyxl example seems to be:
from openpyxl import load_workbook
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['big_data']
for row in ws.rows:
for cell in row:
print(cell.value)
However that just prints out the cell values.
How do I access column data such as
MyColumn = "C"
ThisRow.column(MyColumn).value
A newbie focused example would be really appreciated.
Thanks
from openpyxl import load_workbook
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['big_data']
for row in ws.rows:
????? - How to access Column C for this row??
Thanks
#!/usr/bin/env python3
import openpyxlwb = openpyxl.load_workbook(filename='example.xlsx') ws = wb['Data']
column = ws['A']colValues = []for cell in column[1:]: colValues.append(cell.value) print(cell.value)
print(colValues)
ApplesCherriesPearsOrangesApplesBananasStrawberries['Apples', 'Cherries', 'Pears', 'Oranges', 'Apples', 'Bananas', 'Strawberries']
Warning (from warnings module):File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/reader/worksheet.py", line 312warn(msg)UserWarning: Unknown extension is not supported and will be removed
wb = openpyxl.load_workbook(filename='example.xlsx', read_only=True)
Traceback (most recent call last):File "/Users/Alper/Library/Mobile Documents/com~apple~CloudDocs/Documents/Python/Other code/openpyxl/charles_column.py", line 7, in <module>column = ws['A']File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/worksheet/read_only.py", line 85, in __getitem__return meth(key)File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py", line 363, in __getitem__cols = tuple(self.iter_cols(min_col, max_col))AttributeError: 'ReadOnlyWorksheet' object has no attribute 'iter_cols'
from openpyxl import load_workbookwb = load_workbook(filename='DummyDataFileV1.xlsx', read_only=True)ws = wb['Data']
for row in range(2, ws.max_row + 1):
Fruit= ws['A' + str(row)].value
Quantity= ws['B' + str(row)].value
print ("Fruit: ", Fruit, "Quantity: ", Quantity)
#!/usr/bin/env python3
import openpyxlwb = openpyxl.load_workbook(filename='example_2.xlsx') ws = wb['Data']
for row in ws.iter_rows(min_row=2): print('item:', row[0].value, 'price:', row[2].value, 'origin:', row[3].value)
print()
for row in ws.iter_rows(min_row=2): print('item:', row[0].value.rjust(15), 'price:', str(row[2].value).rjust(5), 'origin:', row[3].value)
item: Apples price: 2.87 origin: CAitem: Cherries price: 0.43 origin: VAitem: Pears price: 3.14 origin: NYitem: Oranges price: 3.78 origin: AZitem: Apples price: 4.17 origin: CAitem: Bananas price: 4.25 origin: ORitem: Strawberries price: 4.99 origin: TXitem: Apples price: 2.87 origin: CAitem: Cherries price: 0.43 origin: VAitem: Pears price: 3.14 origin: NYitem: Oranges price: 3.78 origin: AZitem: Apples price: 4.17 origin: CAitem: Bananas price: 4.25 origin: ORitem: Strawberries price: 4.99 origin: TX