3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

2つのExcelファイルの値の差異を抽出する

Posted at

前回の続き、値の違いを抽出します

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()

新たなファイルとして出力しました、違いが一目でわかるようになってると思います

3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?