简体   繁体   English

使用 python 和 xlsxwriter 以另一种格式将新文本添加到 excel 单元格中

[英]Adding new text into excel cell with another format using python & xlsxwriter

Hello I've recently embarked on a project that allows me to input some data into a python programme using Tinker.您好,我最近开始了一个项目,该项目允许我使用 Tinker 将一些数据输入到 python 程序中。 This the programme interface.这是程序界面。

在此处输入图像描述

With this input after clicking "Go" it'll open an excel spreadsheet and write the start and end time for that specific date.单击“开始”后输入此输入,它将打开 excel 电子表格并写入该特定日期的开始和结束时间。 My question is how do I write a code to have a different colour of text for the NEW text without altering say what was in the cell originally using xlsxwriter?我的问题是如何编写代码以使新文本具有不同颜色的文本而不改变最初使用 xlsxwriter 的单元格中的内容? Here's an example.这是一个例子。

This is the original text/format for say 5th May cell in my excelsheet.这是我的 excelsheet 中 5 月 5 日单元格的原始文本/格式。 在此处输入图像描述

And after clicking Go, I hope to achieve this:而在点击Go之后,希望能做到这一点: 在此处输入图像描述

The coding of opening excel, writing, finding the cell, and saving.打开excel,写入,找到单元格,保存的编码。 I'm ok with that.我没问题。 I hope this is a clear enough question and hopefully it's an answer I can use!我希望这是一个足够清楚的问题,并希望这是我可以使用的答案! Thanks!!谢谢!!

I think the GetCharacters function done on a Range, using win32com (pywincom) will do what you want.我认为 GetCharacters function 在 Range 上完成,使用 win32com (pywincom) 会做你想做的事。

ws.Range(cell/range as string).GetCharacters(start,end).Font.Color = [color ID]

After opening the workbook, I was able to do this to make characters 2-5 as Red:打开工作簿后,我可以将字符 2-5 设为红色:

ws.Range('A1').GetCharacters(2,5).Font.Color = -16776961

I got a lot of this from a previous question looking at bolding: How Do I Bold only part of a string in an excel cell with python我从上一个关于加粗的问题中得到了很多这样的信息: How Do I Bold only part of a string in an excel cell with python

To get the color (and there is probably a better way), I went into Excel and used the macro recorded , and just changed the font the red and saw what the macro recorded called that color.为了获得颜色(并且可能有更好的方法),我进入 Excel 并使用宏记录,并将字体更改为红色,然后看到宏记录的那个颜色。 So you could get the number ID for the colors you want from that.因此,您可以从中获得您想要的 colors 的编号 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM