有没有办法使用 XlsxWriter 为包含文本的单元格着色?
Is there a way to color cells containing text using XlsxWriter?
我有一个 pandas 数据框,我正在使用 to_excel 将其导出到 Excel 文件。所有单元格中都有伪随机生成的长度为 2 的字符串(字母数字或数字)。我想将某些单元格的背景着色为灰色。每次我 运行 .py 文件时,我总是想为完全相同的单元格着色,但是由于单元格的内容基本上是随机的,所以我不能对单元格中的值使用某些条件来为它们着色。
我看了xlsx writer documentation on conditional formatting and tried to use it in conjunction with the accepted answer on 。作为旁注,如果我只是复制粘贴已接受答案的第一个代码块,那 运行 就好了!
这是我的代码:
df = pd.DataFrame({'Col1': ['A1', 'B2', '3C', '4D', 'E5', '6F', 'G7'],
'Col2': ['00', '01', '02', '03', '04', '05', '06']})
writer = pd.ExcelWriter('shaded.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
shadedFormat = workbook.add_format({'bg_color': 'gray'})
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'not equal to',
'value': 'longString',
'format': shadedFormat})
writer.save()
python 解释器没有 return 任何错误,但是当我打开 .xlsx 文件时,弹出窗口 window 给出 "We found a problem with some content in 'shaded.xlsx'. Do you want us to try to recover as much as we can?" 是的,它会显示另一个 windows,它表示 "Repaired Part: /xl/worksheets/sheet1.xml part with XML error. Catastrophic failure Line 2, column 1267." 和 none 的列是有阴影的。
我想消除错误并将指定的单元格显示为灰色。
感谢您的任何 help/suggestions!
错误来自 type
和 criteria
。
尝试:
type
: 'text'
criteria
: 'containing'
示例:所有包含 "C3" 的单元格都是彩色的。
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'containing',
'value': '3C',
'format': shadedFormat})
输出:
更新:
如果你想 select 一个单元格,你可以使用:
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'containing',
'value': '',
'format': shadedFormat})
输出:
我有一个 pandas 数据框,我正在使用 to_excel 将其导出到 Excel 文件。所有单元格中都有伪随机生成的长度为 2 的字符串(字母数字或数字)。我想将某些单元格的背景着色为灰色。每次我 运行 .py 文件时,我总是想为完全相同的单元格着色,但是由于单元格的内容基本上是随机的,所以我不能对单元格中的值使用某些条件来为它们着色。
我看了xlsx writer documentation on conditional formatting and tried to use it in conjunction with the accepted answer on
这是我的代码:
df = pd.DataFrame({'Col1': ['A1', 'B2', '3C', '4D', 'E5', '6F', 'G7'],
'Col2': ['00', '01', '02', '03', '04', '05', '06']})
writer = pd.ExcelWriter('shaded.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
shadedFormat = workbook.add_format({'bg_color': 'gray'})
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'not equal to',
'value': 'longString',
'format': shadedFormat})
writer.save()
python 解释器没有 return 任何错误,但是当我打开 .xlsx 文件时,弹出窗口 window 给出 "We found a problem with some content in 'shaded.xlsx'. Do you want us to try to recover as much as we can?" 是的,它会显示另一个 windows,它表示 "Repaired Part: /xl/worksheets/sheet1.xml part with XML error. Catastrophic failure Line 2, column 1267." 和 none 的列是有阴影的。
我想消除错误并将指定的单元格显示为灰色。
感谢您的任何 help/suggestions!
错误来自 type
和 criteria
。
尝试:
type
: 'text'criteria
: 'containing'
示例:所有包含 "C3" 的单元格都是彩色的。
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'containing',
'value': '3C',
'format': shadedFormat})
输出:
更新:
如果你想 select 一个单元格,你可以使用:
worksheet.conditional_format('B3:B5', {'type': 'text',
'criteria': 'containing',
'value': '',
'format': shadedFormat})
输出: