使用python在单个Excel单元格中格式化单个字符 [英] Format individual characters in a single Excel cell with python

查看:366
本文介绍了使用python在单个Excel单元格中格式化单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python 2.7的Windows Vista操作系统上使用xlrd,xlwt和xlutils。我在一个excel工作表中有一组100个字符长的DNA序列,每个序列都在一个单元格中。我试图在excel(粗体或更改颜色)中突出显示每个这些序列中的特定位置的字符,但是还没有找到一种方法来格式化单元格中的单个字符。根据我的知识,应用风格将其应用于整个单元格。因此,我试图将序列分解成单个组件,其中序列的某些部分将用样式修改,而其他部分将不会被修改,然后将它们整理在单个单元格中的单个字符串中。

I am using xlrd, xlwt, and xlutils on the Windows Vista OS with Python 2.7. I have a set of DNA sequences in an excel worksheet that are 100 characters long, with each sequence in a single cell. I am trying to highlight characters at specific positions within each of these sequences in excel (bold them or change color), but have not found a way to format individual characters within a cell. Applying a style applies it to the entire cell to my knowledge. Therefore I am trying to break the sequences down into individual components where some parts of the sequence will be modified with a style while others won't, and to then collate these back together into a single string in a single cell.

代码:



    rb = open_workbook('Mybook', formatting_info=True)
    rs = rb.sheet_by_index(0)
    wb = copy(rb)
    ws = wb.get_sheet(0)

    minus35style = xlwt.easyxf('font: bold 1') # style I'd like for just one character
    for b in range(0, 368, 1):
        rscellin = rs.cell(b,9)
        f = rscellin.value
        tominus35 = str(f[0:34])
        minus35 = str(f[35:36])
        ws.write(b, 14, tominus35)
        ws.write(b, 14, minus35, minus35style)
    wb.save('Mybook')

我的问题是添加一个样式会改变整个单元格,而我只想修改某些字符。此外,后续写入同一个单元格会覆盖以前的内容。有没有办法修改单个单元格中的单个字符,或者添加不同格式的文本到已经有文本的现有单元格?

My problem is that adding a style changes the whole cell, and I want just certain characters to be modified. Additionally, subsequent writing to the same cell overwrites what was there previously. Is there a way to modify individual characters in a single cell, or to add differently formatted text to an existing cell that already has text in it?

请让我知道您需要我忽略的其他信息。我很感激你提前的时间。

Please let me know if you require additional information that I've overlooked. I appreciate your time in advance.

Brett

推荐答案

xlwt 包括在单元格中使用富文本的功能。通常情况下,您将使用 ws.write ,请改用 ws.write_rich_text 。前两个参数是行索引和列索引,像往常一样;但下一个参数是一系列的组件。每个组件可以是裸体文本值或(文本,字体)对。裸体文本值将使用单元格整体样式的字体,可以使用可选的第四个参数指定。

Recent versions of xlwt include the ability to use Rich Text within a cell. Where normally you would use ws.write, use ws.write_rich_text instead. The first two parameters are the row index and column index, as usual; but the next parameter is a sequence of components. Each component can either be a "naked" text value or a (text, font) pair. The naked text values will use the font from the cell's overall style, which can be specified using the optional fourth parameter.

对于(文本,字体)对,它是最简单的生成字体使用新的 easyfont 功能,这是一种像 easyxf ,但只适用于字体。这是一个例子:

For the (text, font) pairs, it is simplest to generate fonts using the new easyfont feature, which is kind of like easyxf but only for fonts. Here is an example:

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')

font0 = xlwt.easyfont('')
font1 = xlwt.easyfont('bold true')
font2 = xlwt.easyfont('color_index red')
style = xlwt.easyxf('font: color_index blue')

seg1 = ('bold', font1)
seg2 = ('red', font2)
seg3 = ('plain', font0)
seg4 = ('boldagain', font1)

ws.write_rich_text(2, 5, (seg1, seg2, seg3, seg4))
ws.write_rich_text(4, 1, ('xyz', seg2, seg3, '123'), style)

wb.save('rich_text.xls')

您应该能够适应上述目的为您的目的。请注意,您仍然必须一次写入或覆盖整个单元格;您不能再回去,稍后再更新一部分单元格。

You should be able to adapt the above for your purposes. Note that you still have to write or overwrite the whole cell at once; you can't go back and update only part of a cell later.

这篇关于使用python在单个Excel单元格中格式化单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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