2

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! :)

cgm
  • 39
  • 5
  • Are you open to use `VBA` for this task? – Prasanna Apr 03 '18 at 09:16
  • Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Mr. T Apr 03 '18 at 09:22
  • 2
    Possible duplicate of [The ability to apply multiple formats to cell with xlwt / openpyxl](https://stackoverflow.com/questions/8429014/the-ability-to-apply-multiple-formats-to-cell-with-xlwt-openpyxl) – Charlie Clark Apr 03 '18 at 09:57
  • 1
    Possible duplicate of [Editing workbooks with rich text in openpyxl](https://stackoverflow.com/questions/28774757/editing-workbooks-with-rich-text-in-openpyxl) – Norrius Apr 03 '18 at 12:06
  • Do you really want a solution in openpyxl only? – amanb Apr 03 '18 at 20:58
  • @Prasanna: Actually I'm a beginner with Python, so I'm open to any solution. Till now I haven't heard VBA, what would be the advantage of it? – cgm Apr 04 '18 at 16:40
  • @user8212173: I'm a beginner and don't know another way of working on Excel sheets. What else would be your suggestions? – cgm Apr 04 '18 at 16:43
  • VBA is the best choice here as it is integrated within MS Excel. You just need to write a simple subroutine to do this task. Here's an [example](https://stackoverflow.com/questions/7618121/change-color-of-certain-characters-in-a-cell). If you want to use Python for this job, then you can use xlwt. Take a look at: [Format individual characters in a single Excel cell with python](https://stackoverflow.com/questions/14149748/format-individual-characters-in-a-single-excel-cell-with-python) – amanb Apr 04 '18 at 18:06
  • @cgm I have added an answer with VBA. Please see if it takes you in the right path – Prasanna Apr 04 '18 at 18:43

2 Answers2

3

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.

amanb
  • 5,276
  • 3
  • 19
  • 38
0

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.
Prasanna
  • 303
  • 2
  • 16