相关疑难解决方法(0)

使用python有条件地从Excel格式化文本字符串

我想格式化电子表格(xls或xlsx),以便通过用特定颜色填充背景来格式化包含单词或以某个字符串结尾的任何单元格.

例如,如果单元格包含单词"已删除",请将其填充为黑色并将文本绘制为白色.如果单元格以'.pf'结尾,则将单元格绘制为红色.

几年前我发现了类似的问题,提出以下建议:

import xlrd 
import xlutils.copy 

inBook = xlrd.open_workbook('input.xls', formatting_info=True) 
outBook = xlutils.copy.copy(inBook) 

def _getOutCell(outSheet, colIndex, rowIndex): 
    """ HACK: Extract the internal xlwt cell representation. """ 
    row = outSheet._Worksheet__rows.get(rowIndex) 
    if not row: return None 
    cell = row._Row__cells.get(colIndex) 
    return cell 

def setOutCell(outSheet, col, row, value): 
    """ Change cell value without changing formatting. """ 
    # HACK to retain cell style. 
    previousCell = _getOutCell(outSheet, col, row) 
    # END HACK, PART I 
    outSheet.write(row, col, value) 
    # HACK, PART II 

    if previousCell: 
        newCell = …
Run Code Online (Sandbox Code Playgroud)

python excel conditional-formatting

5
推荐指数
1
解决办法
3437
查看次数

标签 统计

conditional-formatting ×1

excel ×1

python ×1