openpyxl在同一单元格中更改一个单词的颜色 [英] openpyxl change one word's color in the same cell

查看:653
本文介绍了openpyxl在同一单元格中更改一个单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openpyxl将一些数据写入excel工作表。在我的脚本中,我需要将数据追加到相同的单元格,并为新添加的数据用不同的颜色突出显示。目前,我在下面尝试过,但事实证明所有数据的颜色都会立即更改。是否可以在同一单元格中更改一个单词的颜色?

I am using openpyxl to write some data to excel sheet. In my script, I need to append data to the same cell and highlight with different color for the new added data. Currently, I tried below but it turns out all the data's color will change at once. Is there any way to change one word's color in the same cell?

from openpyxl import Workbook
from openpyxl.styles import Font
from openpyxl.styles import colors
book = Workbook()
sheet = book.active
sheet.cell(row=1, column=2).value = "11111"
sheet.cell(row=1, column=2).font = Font(color=colors.GREEN)
sheet.cell(row=1, column=2).value += " 12345"
sheet.cell(row=1, column=2).font = Font(color=colors.RED)
sheet.cell(row=1, column=2).value += " 22222"


推荐答案

不幸的是,这对于openpyxl是不可能的。您必须使用 xlwt (对于旧版Excel格式)或 xlsxwriter (对于当前Excel格式)。

This is unfortunately not possible with openpyxl. You'll have to use xlwt (for the legacy Excel format) or xlsxwriter (for the current Excel format).

请参见 https:// foss。 heptapod.net/openpyxl/openpyxl/-/issues/1503 来确认openpyxl不(可能永远不会)支持此功能。

See https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1503 for confirmation that openpyxl does not (and likely will never) support this functionality.

有关<$的更多信息c $ c> xlwt 参见 https://pypi.org/project/xlwt/

有关 xlsxwriter 的更多信息,请参见> https://pypi.org/project/XlsxWriter/

For more information on xlsxwriter see https://pypi.org/project/XlsxWriter/.

这篇关于openpyxl在同一单元格中更改一个单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆