Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with applying Styles in - OpenPyXL

I am trying to style specific rows and columns.

worksheet.cell(row=file_row_number, column=1).value = "Hotel ID"
_cell = worksheet.cell("C1")
_cell.style.font.bold = True

It shows me error

TypeError: cannot set bold attribute

Previously I was using XLWT and it had very easy method of applying styles like you define style variable once and then for ever write() yo can just pass the style variable to apply style on that row/column

Is there any way in OpenPyXL to apply style easily as I mentioned above?

like image 453
Umair Ayub Avatar asked Dec 25 '22 02:12

Umair Ayub


1 Answers

This has changed since the accepted answer was given.

If you do what the accepted answer suggests, you will receive this warning: UserWarning: Use formatting objects such as font directly warn("Use formatting objects such as font directly")

The answer still works but the new solution would be to apply it to the cell directly, for example: worksheet.cell("C1").font = Font(bold=True)

like image 140
Mark Avatar answered Jan 03 '23 14:01

Mark