probat
probat

Reputation: 1532

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.

Upvotes: 28

Views: 65980

Answers (3)

jmh
jmh

Reputation: 480

As of Version 3.1 openpyxl now supports a CellRichText object that supports this functionality (see documentation).

from openpyxl import Workbook
from openpyxl.cell.text import InlineFont
from openpyxl.cell.rich_text import TextBlock, CellRichText

# create rich text cell
cell_text = CellRichText(
    TextBlock(InlineFont(b=True, sz=24), 'Bolded text'), 
    ' - other text',
)

# assign it to sheet
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = cell_text

enter image description here

Credit to @stansy for sharing this in a comment to another answer here.

Upvotes: 3

bbebin
bbebin

Reputation: 61

I do not think openpyxl has implemented any handling for intracell style differences. If you take a file that has multiple styles in one cell, import it in openpyxl and save it new file, without changing the cell, the new file will lose its formatting.

>>>import openpyxl
>>>path = 'test.xlsx'
>>>book = openpyxl.load_workbook(path)
>>>book.active['a1']
>>>book.active['a1'].value
'not bold line\nbold line\nnot bold line\n'
>>>print(_)
not bold line
bold line
not bold line

>>>book.save(path)

image with bold

enter image description here

From here you have a couple options.

  1. Use another lib for handling xlsx. I will refrain from talking about using alternative libraries because I'm presuming you are using openpyxl for a reason.

  2. Create your own markdown and use a custom script to add the formatting after editing and saving the file with openpyxl. You can find the spec for Office Open XML File Formats here. Here is a also a great article about parsing xlsx files and xml.

  3. edit the codebase for openpyxl.

  4. If the steps above are not achievable/desirable, try to contact the openpyxl maintainers, or hire help :/

Upvotes: 2

flywire
flywire

Reputation: 1365

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

Upvotes: 37

Related Questions