Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: openpyxl change font to bold

I'm using Python version 3.6 and the latest version of the openxlpy module (v2.4.8) on Windows.

I want to change certain font to bold in a cell, but I don't want all the text contained in the cell to be bold. In short, I'm saving data to a new Excel workbook that I've created using openxlpy. I'm saving multiple lines of data in one cell. I only want the first line of each cell to be bold.

I've searched everywhere in the openpyxl documentation and online but I can't find anything. It appears to me that you can only apply font styling to the entire cell which doesn't seem right. In Microsoft Excel you can apply different font styles to different data within one cell.

In summary, I want to only bold certain text in a cell and not bold the entire contents of the cell.

like image 560
probat Avatar asked Sep 11 '17 18:09

probat


People also ask

How do I style a cell in Openpyxl?

To change a style property of a cell, first you either have to copy the existing style object from the cell and change the value of the property or you have to create a new style object with the desired settings. Then, assign the new style object to the cell.

How do I write in Openpyxl?

Openpyxl write to a cell There are two basic ways to write to a cell: using a key of a worksheet such as A1 or D3, or using a row and column notation with the cell method. In the example, we write two values to two cells. Here, we assing a numerical value to the A1 cell.


1 Answers

Answers post title but not ops specific question.

from openpyxl.workbook import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
ws['B3'] = "Hello"
ws['B3'].font = Font(bold=True)
wb.save("BoldDemo.xlsx")

Screendump of openpyxl BoldDemo

like image 110
flywire Avatar answered Oct 08 '22 16:10

flywire