-2

How to make the same as the picture with openpyxl. I tried but I couldn't find a way to do it. here is the code I tried

from openpyxl.styles import Alignment, Font, Color
from openpyxl.drawing.image import Image
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active()
ws['C1'] = "TestTest"
ws['C1'].font = Font(name='trebuchet ms', color='EE7806', size=16, bold=True)

i don't know how to create the text as you see in the picture every word has it own style in the same cell enter image description here

thanks

Hosam Gamal
  • 163
  • 1
  • 2
  • 12
  • Please see [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/ask) from the [intro tour](https://stackoverflow.com/tour). "*Show me how to solve this coding problem*" [is off-topic for Stack Overflow](https://meta.stackoverflow.com/q/284236). You have to [make an honest attempt at the solution](https://meta.stackoverflow.com/q/261592), and then ask a specific question about your implementation. – MattDMo Aug 07 '21 at 11:16
  • Stack Overflow is not a code-writing service where you can just drop your requirements and expect someone to fulfill them free of charge. Please [edit] your question and show us what you've tried, what the results were, if any, and the *full text* of any errors or tracebacks. Do you know how to write to a particular cell? Do you know how to change the font and size? Do you know how to change the color? Please show us what you *do* know how to do, and we can help you from there. – MattDMo Aug 07 '21 at 13:56
  • I searched a lot and the link you shared doesn't help me I use openpyxl not pandas or xlsxwriter – Hosam Gamal Aug 07 '21 at 14:38
  • Then why not use `xlsxwriter`? Use the right tool for the job. – MattDMo Aug 07 '21 at 14:53
  • because the whole project is built with openpyxl and it is the main requirement for the project. i did edit the question can you answer it now? – Hosam Gamal Aug 07 '21 at 15:07

1 Answers1

1

What you are looking for is rich text. Unfortunately, openpyxl does not, and apparently will not, support it. From this (closed) issue in the OpenPyXL repo: "This [rich text] cannot be done without adding a great deal of complexity to the library." Charlie Clark, one of the creators of OpenPyXL, says as much in this Stack Overflow answer. So, while you can format text in cells (bolding, choosing a font, coloring the text, etc.), you cannot mix formats such as what you're looking for. You can only apply one set of formatting rules to the entire string of text.

However, the xlsxwriter library does this very task with ease. From this example, you can mix bold and italic in the same cell:

# Copyright 2013-2021, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_column('A:A', 30)

# Set up some formats to use.
bold = workbook.add_format({'bold': True})
italic = workbook.add_format({'italic': True})

worksheet.write_rich_string('A1',
                            'This is ',
                            bold, 'bold',
                            ' and this is ',
                            italic, 'italic')
workbook.close()

I don't know offhand if the OpenPyXL and xlsxwriter Workbook and Worksheet objects are compatible, so you'll need to do some testing. If they aren't, you may need to refactor your code some in order to use the rich text feature of xlsxwriter.

Finally, it's worth noting that there are other Excel-interfacing Python modules, such as PyXLL and XLWings that can handle pretty much any Excel operation or feature because the use COM to tie directly into the program. However, you need a copy of Excel on the computer you're running the script on, and you may need to install plugins into Excel as well.

MattDMo
  • 100,794
  • 21
  • 241
  • 231