簡體   English   中英

在 XlsxWriter 中使用帶有條件格式的 for 循環的問題

[英]Issues with using a for loop with conditional formatting in XlsxWriter

Python 2.7:

我正在嘗試使用 XlsxWriter 將 excel 中包含特定文本的所有單元格加粗。 我已將文本存儲在列表中,並使用 for 循環遍歷元素。 我不確定我是否使用正確的語法來指定 XlsXwriter 提供的conditional_format字典中的“值”鍵的值。 我的字典中包含字符串的單元格沒有被轉換為粗體格式。

header_format = new_wb.add_format({'bold': True})

header_list = ["Issue", "Type", "Status", "Resolution", "Summary", "Priority", "Fix Version", "Labels"]

for i in range(len(header_list)):
    new_ws.conditional_format('A1:Z999', {'type': 'cell', 'criteria': 'equal to', 'value': '"header_list[i]"' , 'format': header_format})


您需要使用 header 字符串作為條件格式的值,並且它們需要雙引號(根據 Excel 的要求)。 您正在嘗試這樣做,但您的語法錯誤。 這是基於您的示例的更正版本:

import xlsxwriter

new_wb = xlsxwriter.Workbook('test.xlsx')
new_ws = new_wb.add_worksheet()

header_format = new_wb.add_format({'bold': True})

header_list = ["Issue", "Type", "Status", "Resolution",
               "Summary", "Priority", "Fix Version", "Labels"]

for value in header_list:
    new_ws.conditional_format('A1:Z999', {'type': 'cell',
                                          'criteria': 'equal to',
                                          'value': '"%s"' % value,
                                          'format': header_format})

# Write some strings to test against.
new_ws.write_column('A1', ['Foo', 'Type', 'Bar', 'Status'])

new_wb.close()

Output 目標詞以粗體顯示:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM