Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

proper way to align cell in python using openpyxl

What is the proper way to align text inside cell? I tried this:

wrsheet.cell("B2").style.alignment.horizontal = "justify"

But it gives me an error: warn("Use formatting objects such as font directly")

like image 743
Newboy11 Avatar asked Oct 31 '15 15:10

Newboy11


1 Answers

In Excel styles are shared between cells. To avoid unexpected side-effects you cannot modify styles once they must always been assigned to be changed. Since openpyxl v2.2 the aggregate Style object is deprecated in favour of the relevant formatting objects such as Font, or in your case Alignment.

from openpyxl.styles import Alignment
ws['B2'].alignment = Alignment(horizontal="justify")
like image 185
Charlie Clark Avatar answered Sep 25 '22 21:09

Charlie Clark