前回の続き、値の違いを抽出します
import pandas as pd
df_NEW = pd.read_excel("NEW_file.xlsx").fillna("null")
df_OLD = pd.read_excel("OLD_file.xlsx").fillna("null")
df_DIFF = df_OLD.copy()
for row in range(df_DIFF.shape[0]):
for col in range(df_DIFFshape[1]):
value_OLD = df_OLD.iloc[row,col]
value_NEW = df_NEW.iloc[row,col]
if value_OLD == value_NEW:
df_DIFF.iloc[row,col] = df_NEW.iloc[row,col]
else:
df_DIFF.iloc[row,col] = ("{}→{}").format(value_OLD,value_NEW) #値が違う場合、{古い値}→{新しい値}のように書き換える
writer = pd.ExcelzWriter("DIFF_file.xlsx",engine="xlsxwriter")
df_DIFF.to_excel(writer,sheet_name="DIFF")
df_NEW.to_excel(writer,sheet_name="NEW")
df_OLD.to_excel(writer,sheet_name="OLD")
workbook = writer.book
worksheet = writer.sheets["DIFF"]
#以下フォント、色の処理
gray_fmt = wookbook.add_format({"font_color":"#E0E0E0"})
highlight_fmt = wookbook.add_format({"font_color":"#E0E0E0",
"bg_color":"#B1B3B3"})
worksheet.conditional_format("A1:ZZ10000",{"type":"text",
"criteria":"containing",
"value":"→",
"format":highlight_fmt})
worksheet.conditional_format("A1:ZZ10000",{"type":"text",
"criteria":"not containing",
"value":"→",
"format":gray_fmt})
#保存
writer_save()
新たなファイルとして出力しました、違いが一目でわかるようになってると思います