首页 > 解决方案 > 通过python保存文件后现有条件格式被破坏

问题描述

我正在使用 python 中的代码打开一个现有的具有条件格式的 Excel 文件。代码打开文件,在其中添加一些数字,并以不同的名称将文件保存在不同的文件夹中。当我打开保存的新文件时,条件格式存在但工作方式不同。条件格式是一个数据栏,我在其中设置了最小值零和最大值 1。问题是最小值和最大值在新文件中不再保留,因为它们是在初始文件中设置的。具体来说,值为 0 时,条不完全为空(显示短条),而值为 1 时,条未完全满。

我确信导致这种情况的原因与文件中添加的数字无关,因为我在没有这一步的情况下运行了代码,问题仍然存在。

这是我正在使用的代码:

xfile = openpyxl.load_workbook('Output Predictions Prototype File.xlsx') # Opens the original excel output prediction file
sheet = xfile.get_sheet_by_name('Output Predictions') # Chooses the 'Output Prediction' sheet in the file

sheet['C5'] ='Report Date: ' + currentYear + '-' + currentMonth + '-' + currentDay #inserts report date in the format YYYY-MM-DD

Entity_name_and_code = Entity_names[i] + ' - (' + Entity_codes[i] +')' # Creates string with entity name and entity code
sheet['C7'] =Entity_name_and_code

sheet['D9'] = notch_to_rating_class[rating_predictions[i]] # Inserts the predicted rating in cell D9

# Inserts the predicted probabilities
sheet['D12'] = probabilities[i][4]
sheet['D13'] = probabilities[i][3]
sheet['D14'] = probabilities[i][2]
sheet['D15'] = probabilities[i][1]
sheet['D16'] = probabilities[i][0]

sheet['E19'] = 13-NA_counter_for_bank_i     # Inserts the number of variables with availiable data
sheet['E20'] = NA_counter_for_bank_i     # Inserts the number of variables with N/A data


filename='CFS_Prediction_' + Current_Year_Month_Day + '_' + Entity_codes[i] + '.xlsx'    #Stores the name of the file in a string    
file_path = 'C:\\Users\\Downloads\\Python Files - Projects\\Output Prediction Files\\'    #File path were we want to save the files in
complete_file_name = file_path + filename     # Complete file path with the file name at the end
xfile.save(complete_file_name)

标签: pythonformattingconditional-statements

解决方案


推荐阅读