简体   繁体   English

如何使用XlsxWriter根据另一个工作表中的相应单元格值来格式化一个工作表中的所有单元格?

[英]How to format all cells in one sheet based on corresponding cell values in another sheet using XlsxWriter?

I am exporting to Excel two pandas DataFrames using XlsxWriter, each DataFrame having a separate sheet. 我正在使用XlsxWriter将两个熊猫DataFrames导出到Excel,每个DataFrame都有一个单独的工作表。 I would like to apply a colour format to all the cells in one sheet based on values in another sheet, they correspond one-to-one based on column names and row numbers. 我想根据另一张纸中的值将一种颜色格式应用于一张纸中的所有单元格,它们基于列名和行号一对一地对应。

Example: 例:

Sheet1

  A B C
1 1 3 1
2 0 4 2

Sheet2

  A B C
1 a d b
2 b a a

I would like to assign a colour format to all the cells in Sheet1 that have a value 'a' in Sheet2. 我想为Sheet1中所有在Sheet2中具有值“ a”的单元格分配一种颜色格式。

The first step is to work out the conditional format in Excel and then transfer it to XlsxWriter. 第一步是在Excel中计算条件格式,然后将其传输到XlsxWriter。

Something like the following should work: 类似于以下内容的东西应该起作用:

import pandas as pd

# Create some Pandas dataframes from some data.
df1 = pd.DataFrame([[ 1,   3,   1 ], [ 0,   4,   2 ]])
df2 = pd.DataFrame([['a', 'd', 'b'], ['b', 'a', 'a']])

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_xlsxwriter.xlsx', engine='xlsxwriter')

# Write each dataframe to a different worksheet.
df1.to_excel(writer, sheet_name='Sheet1', header=False, index=False)
df2.to_excel(writer, sheet_name='Sheet2', header=False, index=False)

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Create a format for the conditional format.
condition_format = workbook.add_format({'bg_color': '#C6EFCE',
                                        'font_color': '#006100'})

# Write a conditional format over a range.
worksheet.conditional_format('A1:C2', {'type': 'formula',
                                       'criteria': '=Sheet2!A1="a"', 
                                       'format': condition_format})

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output: 输出:

在此处输入图片说明

Note that Excel requires double quotes around the string in the formula =Sheet2!A1="a" and by using a relative cell reference ( A1 not the absolute value $A$1 ) the formula is applied individually to each cell in the range. 请注意,Excel需要在公式=Sheet2!A1="a"的字符串周围加上双引号,并且通过使用相对单元格引用A1不是绝对值$A$1 ),该公式将分别应用于范围中的每个单元格。

You can apply the conditional format to a range based on the dataframe like this: 您可以将条件格式应用于基于数据框的范围,如下所示:

# Write a conditional format over a range.
start_row = 0
start_col = 0
end_row = df1.shape[0] -1
end_col = df1.shape[1] -1

worksheet.conditional_format(start_row, start_col, end_row, end_col, 
                             {'type': 'formula',
                              'criteria': '=Sheet2!A1="a"', 
                              'format': condition_format})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM