Converts an Excel file to a text file. The purpose is to diff with cli.
You don't need to be able to restore Excel contents from a text file for diff purposes. If you use CSV, even if one line becomes long and diffs, it is very difficult to see the difference on the command line, so I want to make one line for each cell.
I couldn't find a tool that seemed to be convenient, and it was faster to write a script, so I wrote it.
Use a library called openpyxl in Python.
$ pip install openpyxl
This is the only source code.
import sys
import openpyxl
filepath = sys.argv[1]
sheetname = ""
if len(sys.argv) > 2:
sheetname = sys.argv[2]
wb = openpyxl.load_workbook(filepath)
for ws in wb:
if sheetname != "" and ws.title != sheetname:
continue
print(f"sheet:{ws.title}")
for row in ws:
print(" row")
for cell in row:
if cell.value == None:
print("")
else:
print(f" {cell.value}")
If you save it with a name like xls2txt.py
, the contents of all the sheets included in the Excel file will be output as text below.
$ python xls2txt.py foo.xls
If you want to see only a specific sheet, specify the sheet name.
$ python xls2txt.py foo.xls Sheet1
To see the diff, for example:
$ diff -U5 <(python xls2txt.py foo.xls Sheet1) <(python xls2txt.py bar.xls Sheet1) | less
See here if you want to make the difference in diff easier to see. How to display the diff command in color to make it easier to see
Recommended Posts