openpyxl change one word's color in the same cell

空扰寡人 提交于 2019-12-02 04:02:09

问题


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"

来源:https://stackoverflow.com/questions/49522658/openpyxl-change-one-words-color-in-the-same-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!