i am writing / changing a cell-value in an existing excel-sheet using openpyxl and formating the cell as bold using the following code:
import openpyxl
from openpyxl.styles import Font
workbook = openpyxl.load_workbook("try.xlsx")
worksheet = workbook.active
worksheet['C1'] = "this is some text is use"
bold_font = Font(bold=True)
worksheet['C1'].font = bold_font
workbook.save("try.xlsx")
workbook.close()
Now i want to format only the first word "is" and "text" in the cell as bold - rest should stay as normal font. How can i do this using openpyxl=
It's possible now to bold inline text in the excel sheet cell using OpenPyXL.
Here is the link, Working with Rich Text
All you need is Rich Text object, all inline font style is achievable with this feature, as per official documentation.
Rich Text objects can contain a mix of unformatted text and TextBlock objects that contains an InlineFont style and a the text which is to be formatted like this. The result is a CellRichText object.
Support for this feature started from Version 3.1.0(2023-01-31)
Here is an example,
from openpyxl.cell.text import InlineFont
from openpyxl.cell.rich_text import TextBlock, CellRichText
rich_string1 = CellRichText(
'This is a test ',
TextBlock(InlineFont(b=True), 'xxx'),
'yyy'
)
Output of the above code -
This is a test xxxyyy