Convert Excel file to text in Python for diff purposes

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

Convert Excel file to text in Python for diff purposes
Convert psd file to png in Python
I made a script in Python to convert a text file for JSON (for vscode user snippet)
Convert HTML to text file
Speech to speech in python [text to speech]
Convert markdown to PDF in Python
[Python] How to change EXCEL file saved in xlsb to xlsx
[Python] How to convert db file to csv
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert Excel data to JSON with python
Convert from Markdown to HTML in Python
Convert "number" of excel date to python datetime
Convert PDFs to images in bulk with Python
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
[Python] Implemented automation in excel file copying work
Convert UTF-8 CSV files to read in Excel
Try to calculate RPN in Python (for beginners)
Convert exponential notation float to str in Python
Sample to put Python Kivy in one file
Convert cubic mesh code to WKT in Python
Python> List> Convert relative paths to absolute paths> all_filepaths = [datas_path + fp for fp in train_filepaths]
Clustering text in Python
File processing in Python
Text processing in Python
File operations in Python
Python programming in Excel
Convert json to excel
Convert timezoned date and time to Unixtime in Python2.7
Tool to make mask image for ETC in Python
[For beginners] How to use say command in python!
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
How to convert Python # type for Python super beginners: str
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert CIDR notation netmask to dotted decimal notation in Python
How to run python in virtual space (for MacOS)
Convert a text file with hexadecimal values to a binary file
[Python] Convert PDF text to CSV page by page (2/24 postscript)
A memorandum to run a python script in a bat file
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
I want to randomly sample a file in Python
[Python] Change standard input from keyboard to text file
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
[Work efficiency] How to change file names in Python
How to convert Json file to CSV format or EXCEL format
Python OpenCV tried to display the image in text.
How to read text by standard input or file name specification like cat in Python
Assigned scaffolding macro in Python script file to F12 key
UTF8 text processing in python
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Change the standard output destination to a file in Python
Login to website in Python
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Search for strings in Python