如何使用XLWT Python Excel将样式应用于整个行? [英] How do I apply a style to the whole row using XLWT Python Excel?

查看:95
本文介绍了如何使用XLWT Python Excel将样式应用于整个行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用一种样式,如果其中一列包含值资产",则该样式将突出显示整个行.下面的代码将仅突出显示其中包含资产"的列,而不突出显示整个行.有没有一种方法可以将样式应用于整个行?

I'm trying to apply a style that will highlight the whole row if one of the columns contains the value "Assets". The code below will highlight only the column with "Assets" in it, instead of the entire row. Is there a way to apply the style to the whole row?

for row in csv_input:
     #Iterate through each column
      for col in range(len(row)):
           #Apply different styles depending on row
           if row_count == 0:
               sheet.write(row_count,col,row[col],headerStyle)
           elif row_count == 3:
               sheet.write(row_count,col,row[col],subheadStyle)
           elif "Assets" in row[col]:
               sheet.write(row_count,col,row[col],highlightStyle)             
           else:
               if (is_number(row[col]) == True):
                   sheet.write(row_count,col,float(row[col]),rowStyle)
               else:
                   sheet.write(row_count,col,row[col],rowStyle)

如您所见,根据所应用的行,我将使用不同的样式.如何使包含关键字资产"的任何行突出显示?谢谢!

As you can see, depending on the row I am applying different styles. How can I make it so that any row that contains the keyword "Assets" will be highlighted? Thanks!

推荐答案

您的主要问题是,代码在行中写入了一些单元格后,正在检查资产".您需要先进行在整个行中使用哪种样式"测试,然后再 在行中写入任何单元格.在 xlwt Row 对象上设置样式无效.这是默认样式,用于没有其他格式应用的单元格.

Your main problem is that your code is checking for "Assets" after it has written some cells in the row. You need to do your "what style to use for the whole row" tests before you write any cells in the row. Setting a style on the xlwt Row object doesn't work; that's a default style for use with cells that don't have any formatting applied otherwise.

其他问题:

包含值资产".以下代码将仅突出显示包含资产"的列

contains the value "Assets". The code below will highlight only the column with "Assets" in it

这是模棱两可的.假设单元格值正好等于股权资产";你想让我做什么?注意:您的代码将突出显示此类单元格及其右侧的单元格.同样不清楚资产"单元格应该是第一个单元格(在您对另一个答案的评论中的示例)还是任何单元格(根据您的代码).

This is ambiguous. Suppose a cell value is exactly equal to "Equity Assets"; what do you want to do? Note: your code will highlight such a cell and those to its right. Also it's not apparent whether the "Assets"-bearing cell should be the first (example in your comment on another answer) or any cell (as per your code).

您对变量名的某些选择使您的代码很难阅读,例如 row 是单元格值的列表,而 col 是列索引.尽可能使用 enumerate().

Some of your choices for variable names make your code very hard to read e.g. row is a list of cell values but col is a column index. Use enumerate() where possible.

尝试这样的事情:

for row_index, cell_values in enumerate(csv_input):
    # Determine what style to use for the whole row
    if row_index == 0:
        common_style = headerStyle
    elif row_index == 3:
        common_style = subheadStyle
    elif "Assets" in cell_values:
        # perhaps elif any("Assets" in cell_value for cell_value in cell_values):
        # perhaps elif cell_values and cell_values[0] == "Assets":
        # perhaps elif cell_values and "Assets" in cell_values[0]:
        common_style = highlightStyle
    else:
        common_style = rowStyle
    # Iterate over the columns
    for col_index, cell_value in enumerate(cell_values):
        if common_style == rowStyle and is_number(cell_value):
            cell_value = float(cell_value)
        sheet.write(row_index, col_index, cell_value, common_style)

我对 is_number 函数感到好奇...我会用这个:

I'm curious about the is_number function ... I'd use this:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

这会自动导致:

        if common_style == rowStyle:
            try:
                cell_value = float(cell_value)
            except ValueError:
                pass

还提出了一个问题,即您是否应该对数字和文本使用不同的样式.

and also raises the question of whether you should perhaps have different styles for numbers and text.

这篇关于如何使用XLWT Python Excel将样式应用于整个行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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