cgm
cgm

Reputation: 39

Is there a way with openpyxl to print a single word **bold**?

I would like to print out an Excel Sheet, in which I'll have a column with a lot of text. Inside of this text I would like to highlight some specific words in bold?

['B1'] = text text text text text text text text bold text text text text

Till now I just found ways to change the hole cell 'B1' to another format. Does anybody have an idea how to highlight a single word in another way? e.g. another color, italic or line breaks?

Thank you! :)

Upvotes: 2

Views: 1899

Answers (2)

Prasanna
Prasanna

Reputation: 303

Okay, since you are open to VBA solution too as per the comment - I'm giving you a minimal example on making a portion of text bold

Sub MakeTextBold()
' Assume that the required text is in cell A1
    ActiveSheet.Range("A1").Select
' Assume that the text you want to make bold starts from
' position 6 in your cell and number of char to be bold = 2
    With ActiveCell.Characters(Start:=6, Length:=2).Font
     .FontStyle = "Bold"
    End With 
End Sub  

You can build up on this example - by

  1. Change the range to the required range
  2. Find out ways of specifying start position and length of characters to be made bold etc.

Upvotes: 0

amanb
amanb

Reputation: 5463

This is a case of adding rich text support in openpyxl which has been on hold for a while due to complexity issues explained in this Q&A from the official openpyxl mailing list.

Rich Text differs from plain text in that it supports text formatting, such as bold, italics, and underlining, as well as different fonts, font sizes, and colored text. Currently, the latest version of openpyxl(2.5.1) does not support rich text.

Upvotes: 3

Related Questions