0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[スニペット]openpyxl でsaveしたExcelの比較

Last updated at Posted at 2025-04-04
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
import math

# 許容誤差(丸め誤差のため)
TOLERANCE = 1e-9

def extract_numeric_values(ws):
    values = {}
    for row in ws.iter_rows(values_only=True):
        for col_index, val in enumerate(row):
            if isinstance(val, (int, float)):
                col_letter = get_column_letter(col_index + 1)
                cell_ref = f"{col_letter}{row[0].row if hasattr(row[0], 'row') else '?'}"
                values[cell_ref] = val
    return values

# ① 元ファイルの読み込み
wb_original = load_workbook("original.xlsx", data_only=True)
ws_original = wb_original.active
original_values = extract_numeric_values(ws_original)

# ② 保存後ファイルの読み込み
wb_saved = load_workbook("saved.xlsx", data_only=True)
ws_saved = wb_saved.active
saved_values = extract_numeric_values(ws_saved)

# ③ 比較
print("=== 差分チェック結果 ===")
for cell, original_val in original_values.items():
    saved_val = saved_values.get(cell)
    if saved_val is None:
        print(f"{cell}: 元にあったが保存後に消失")
    elif not math.isclose(original_val, saved_val, rel_tol=TOLERANCE):
        print(f"{cell}: 値が異なります → 元: {original_val}, 保存後: {saved_val}")

for cell in saved_values:
    if cell not in original_values:
        print(f"{cell}: 保存後に新たに出現(元にはなかった)")

from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
import math

TOLERANCE = 1e-9

def extract_numeric_values(ws):
    values = {}
    for row in ws.iter_rows(values_only=False):
        for cell in row:
            if isinstance(cell.value, (int, float)):
                values[cell.coordinate] = cell.value
    return values

# ① 元ファイルの読み込み
wb_original = load_workbook("original.xlsx", data_only=True)
ws_original = wb_original.active
original_values = extract_numeric_values(ws_original)

# ② 保存後ファイルの読み込み
wb_saved = load_workbook("saved.xlsx", data_only=True)
ws_saved = wb_saved.active
saved_values = extract_numeric_values(ws_saved)

# ③ 比較
print("=== 差分チェック結果 ===")
diff_found = False
for cell, original_val in original_values.items():
    saved_val = saved_values.get(cell)
    if saved_val is None:
        print(f"{cell}: 元にあったが保存後に消失")
        diff_found = True
    elif not math.isclose(original_val, saved_val, rel_tol=TOLERANCE):
        print(f"{cell}: 値が異なります → 元: {original_val}, 保存後: {saved_val}")
        diff_found = True

for cell in saved_values:
    if cell not in original_values:
        print(f"{cell}: 保存後に新たに出現(元にはなかった)")
        diff_found = True

if not diff_found:
    print("差分は見つかりませんでした。")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?