28

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.

3
  • Can you loop through the characters in a cell with openxlpy? Perhaps get a cell's data/info, then loop through the characters in that?
    – BruceWayne
    Commented Sep 11, 2017 at 18:31
  • 1
    I could loop through the characters.I don't think that would help me though, I think with openpyxl you have to set the entire cell's contents bold for example. I'm hoping someone tells me otherwise.
    – probat
    Commented Sep 11, 2017 at 18:39
  • 4
    Possible duplicate of Editing workbooks with rich text in openpyxl Commented Sep 12, 2017 at 7:23

3 Answers 3

37

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

2
2

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.

1

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 :/

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.