Search code examples
pythonexcelcolorscellopenpyxl

'Worksheet' object has no attribute 'cell'


I need to open an xlsx document and color it. But I don't understand why it shows cell error it. My algorithm works like this:

Open xlsx, writer = pd.ExcelWriter(path, engine='xlsxwriter')

worksheet = writer.sheets['Sheet1']

col_style = Font(name = "Reem Kufi", size = 12, color = "DB3B22", italic =True)

for i in range(2,40):
    worksheet.cell(row = i, column = 3).font = col_style

Error:- 'Worksheet' object has no attribute 'cell'


Solution

  • you will need to use Openpyxl's loadworkbook instead of ExcelWriter to achieve what you are looking for. Updated code here. Note that I have only changed the initial open file and sheet using the new code and required libraries and not changed rest of your code.

    from openpyxl.styles import Font
    from openpyxl import load_workbook
    writer = load_workbook(filename='YourFile.xlsx')
    
    worksheet = writer['Sheet1']
    
    col_style = Font(name = "Reem Kufi", size = 12, color = "DB3B22", italic =True)
    
    for i in range(2,40):
        worksheet.cell(row = i, column = 3).font = col_style
        
    writer.save('YourFile.xlsx')