Python: openpyxl change font to bold

probat picture probat · Sep 11, 2017 · Viewed 14.7k times · Source

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.

Answer

flywire picture flywire · Apr 22, 2018

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