I want to iterate over a sheet and fill in cells if the cell is not 'empty'/null
if the cell is empty then no action required
if the cell is !empty, then color (fill) it in
the error message is
sheet[i].fill = PatternFill(fill_type='solid', start_color='43e33b', end_color='43e33b') AttributeError: 'tuple' object has no attribute 'fill'
The code I'm using is
import openpyxl as xl
from openpyxl.styles import Font, Fill, Color, colors, PatternFill
# creating a variable to take any filename input from user
filename = input('Enter filename here: ')
# loading workbook on local computer c drive using filename
wb = xl.load_workbook(f'c:\\Users\\Charlie\\Desktop\\{filename}.xlsx')
# working with sheet1 on wb 'workbook'
sheet = wb['Sheet1']
# defining pacing list function
def packing_list():
# deleting columns so that columns required are left for new file
sheet.delete_cols(1, 14)
max_rows = sheet.max_row
print(max_rows)
for i in range(1, max_rows):
if sheet.cell(column=7, row=i, value=""):
sheet[i].fill = PatternFill(fill_type='solid',
start_color='43e33b', end_color='43e33b')
else:
break
# saving new worksheet to desktop with name packing_list
wb.save('c:\\Users\\Charlie\\Desktop\\packing_list.xlsx')
sheet[i]
is a tuple containing all the cells in the i
th row, where i >= 1
sheet[i][j]
is the i,j
th cell, where j >= 0. So the 7th column would be sheet[i][6]
.
You could get the same thing using sheet.cell(column=7, row=i)
, where both row and column are 1-based.
You should be able to set sheet.cell(column=7, row=i).fill
without any problems