same parameter resulted in different binary
I want to be able to read .xlsx
file, edit, and save. But, I wanted if the data didn't changes after the edit, the resulted xlsx
file should have the exact binary data.
This currently my inspection and I don't know where is the cause that changes the binary data
import copy
import os
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter
from zipfile import ZipFile, ZIP_DEFLATED
def save_workbook(workbook, filename):
"""Save the given workbook on the filesystem under the name filename.
Unlike openpyxl.writer.excel.save_workbook, it didn't update workbook.properties.modified
:param workbook: the workbook to save
:type workbook: :class:`openpyxl.workbook.Workbook`
:param filename: the path to which save the workbook
:type filename: string
:rtype: bool
"""
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
writer = ExcelWriter(workbook, archive)
writer.save()
return True
file_path = '../xlsx/default.xlsx'
if not os.path.isfile(file_path):
df.to_excel(file_path)
with open(file_path, 'rb') as file:
first_read_binary_data = file.read()
workbook = load_workbook(file_path)
print('Original: \t', workbook.properties.created, workbook.properties.modified)
first_read_properties = copy.deepcopy(workbook.properties)
save_workbook(workbook, file_path)
print('After save: \t', workbook.properties.created, workbook.properties.modified)
workbook = load_workbook(file_path)
print('Re-read save: \t', workbook.properties.created, workbook.properties.modified)
second_read_properties = copy.deepcopy(workbook.properties)
with open(file_path, 'rb') as file:
second_read_binary_data = file.read()
print(first_read_properties == second_read_properties)
print(first_read_binary_data == second_read_binary_data)