有没有办法通过Python触发excel文档中的格式设置
我正在编写一个自动excel工作表比较python脚本,但在打开excel文档时,我很难显示所做的更改。基本上,它会进行单元格级别的比较,如果单元格发生变化,则添加红色填充,如果单元格未发生变化,则添加绿色填充。我正在使用openpyxl编辑值,并更改填充颜色。代码运行后,我打开excel文件看不到任何更改。但是,当我单击单元格本身时,我可以看到颜色填充的变化。我需要帮助找到一个解决方案,该解决方案将在我打开excel文件之前自动触发该格式显示。有人有这方面的经验吗 要运行,请创建一个名为“test”的excel文件,然后将值添加到Sheet1第一列的行中。创建第二张图纸2,并为第一列的一半添加相同的值,然后更改第一列的另一半的值。保存、关闭文件、运行。然后寻找变化有没有办法通过Python触发excel文档中的格式设置,python,excel,pandas,openpyxl,Python,Excel,Pandas,Openpyxl,我正在编写一个自动excel工作表比较python脚本,但在打开excel文档时,我很难显示所做的更改。基本上,它会进行单元格级别的比较,如果单元格发生变化,则添加红色填充,如果单元格未发生变化,则添加绿色填充。我正在使用openpyxl编辑值,并更改填充颜色。代码运行后,我打开excel文件看不到任何更改。但是,当我单击单元格本身时,我可以看到颜色填充的变化。我需要帮助找到一个解决方案,该解决方案将在我打开excel文件之前自动触发该格式显示。有人有这方面的经验吗 要运行,请创建一个名为“te
import pandas as pd
import openpyxl as op
from openpyxl.utils.cell import get_column_letter
from openpyxl.styles import PatternFill
def main():
old_tab = "Sheet1"
new_tab = "Sheet2"
# set up A1:A10 on Sheet1 all to be = 10
# ... then on Sheet2, have A1:A5 be = 10, and A6:A10 be = 20
path = './test.xlsx'
# set up list to track indices that should be highlighted red
cells_to_highlight_red = []
red_fill = PatternFill(fill_type=None, start_color='FFFFFFFF', end_color='FF0000')
# set up list to track indices that should be highlighted green
cells_to_highlight_green = []
green_fill = PatternFill(fill_type=None, start_color='FFFFFFFF', end_color='008000')
old_sheet = pd.read_excel(path, sheet_name=old_tab, data_only=True, header=None).fillna('-')
new_sheet = pd.read_excel(path, sheet_name=new_tab, data_only=True, header=None).fillna('-')
# do cell by cell comparison to see if cells have changed
bool_df = old_sheet.eq(new_sheet)
# go through each column
for col_index in range(bool_df.shape[1]):
# then through each row of the bool_df.
# ... if the cell is False, that means a change has occurred
# ... if the cell is not False, so True, that means no
for row_index, row in enumerate(bool_df.iloc[:,col_index].values.flatten()):
if row == False:
col_letter = get_column_letter(col_index+1)
trg_cell = col_letter + str(row_index+1)
trg_cell.replace(" ", "")
# if this is true, then there was no value to begin or end, so do not add to list to track
if old_sheet.iloc[row_index, col_index] == "-" and new_sheet.iloc[row_index, col_index] == "-":
continue
cells_to_highlight_red.append(trg_cell)
else:
col_letter = get_column_letter(col_index+1)
trg_cell = col_letter + str(row_index+1)
trg_cell.replace(" ", "")
# if this is true, then there was no value to begin or end, so do not add to list to track
if old_sheet.iloc[row_index, col_index] == "-" and new_sheet.iloc[row_index, col_index] == "-":
continue
cells_to_highlight_green.append(trg_cell)
target_workbook = op.load_workbook( filename=path )
target_sheet = target_workbook["Sheet2"]
for trg_col_row in cells_to_highlight_red:
cell_to_edit = target_sheet[trg_col_row]
cell_to_edit.fill = red_fill
for trg_col_row in cells_to_highlight_green:
cell_to_edit = target_sheet[trg_col_row]
cell_to_edit.fill = green_fill
target_workbook.save( path )
main()
你能不能试着把你的代码简化成一个小例子来说明出了什么问题?例如,只需取出excel文件中没有添加颜色的所有内容,以便有人可以更轻松地帮助您。现在,SO上没有人能够运行该代码并复制您的结果。如果您减少代码,您更有可能得到响应。您能否尝试将代码减少到出错的小示例中?例如,只需取出excel文件中没有添加颜色的所有内容,以便有人可以更轻松地帮助您。现在,没有人能够在SO上运行该代码并复制您的结果。如果您减少代码,您就更有可能得到响应。