[PYTHON] From Excel file to exe and release of tool that spits out CSV

Introduction

Since I made a tool to generate CSV from an Excel file in a batch including sheets with Python Here are the steps to release it.

Tool contents

Click here for the tool created this time We also have an exe so that it can be used outside the program environment. For the exe file, put it in the folder containing Excel and double-click it.

exe https://github.com/InTack2/csv-masher/releases

GitHub

https://github.com/InTack2/csv-masher

2020/05/09 postscript I'm sorry! !! It remained private while writing that it was released. .. released. .. ..

Background

When I work as a TA, I often prepare CSV files and Json files, and I generally have them. However, if it is a CSV file, it is very difficult to input that the cell width is broken. .. So, I manage it with Excel .. This time I save it as a different name and the input destination changes, so it is troublesome anyway .. So while making a tool to automate this area, By the way, I wanted to do the public release of GitHub, so I made it.

Development environment

Tool contents

The content is fairly simple. It's like reading an excel file with the openpyxl library and spitting out each sheet with csv. I will omit the explanation etc., but if there is a request ...

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Convert Excel files in the same directory to csv at once
"""
import os
import sys
import csv
import openpyxl
#PyInstaller__file__Problem avoidance
if hasattr(sys, "frozen"):
    SEARCH_FILE_DIRECTORY = os.path.dirname(sys.argv[0])  # "..\\[exe_folder]"
#When the file is executed
else:
    SCRIPT_PATH = os.path.dirname(__file__)  # "..\\src\\csv-masher"
    SRC_PATH = os.path.dirname(SCRIPT_PATH)  # "..\\src"
    SEARCH_FILE_DIRECTORY = os.path.join(SRC_PATH, "sample_file")  # "..\\src\\Sample"
EXCEL_EXTENSION = ".xlsx"
CSV_EXTENSION = ".csv"

class CSVMasher(object):
    """Main generation function
    """
    def __init__(self):
        self.__excel_files = []
        self.set_excel_files(self.__search_excel_file(SEARCH_FILE_DIRECTORY))
    def create_csv_file(self):
        """Generate csv file
        """
        for excel_file in self.__excel_files:
            excel_name = os.path.basename(excel_file).split(".")[0]
            excel_current_directory = os.path.dirname(excel_file)
            book = openpyxl.load_workbook(excel_file, read_only=True, keep_vba=False)
            for sheet in book.worksheets:
                sheet_name = sheet.title
                export_name = "{excel_name}_{sheet_name}{extension}".format(excel_name=excel_name, sheet_name=sheet_name, extension=CSV_EXTENSION)
                export_csv_path = os.path.join(excel_current_directory, export_name)
                with open(export_csv_path, "w", encoding="utf-8") as f:
                    writer = csv.writer(f)
                    for cols in sheet.rows:
                        writer.writerow([str(_.value or "") for _ in cols])
    def __search_excel_file(self, search_directory):
        """Get an excel file
        """
        hit_files = []
        for dir_path, dir_list, file_list in os.walk(search_directory):
            for file_name in file_list:
                if EXCEL_EXTENSION in file_name:
                    hit_files.append(os.path.join(dir_path, file_name))
        return hit_files
    def get_excel_files(self):
        """Get an excel file
        Returns:
            list:Excel pass list
        """
        return self.__excel_files
    def set_excel_files(self, excel_files):
        """Set the excel file
        Args:
            excel_files (list):excel file path list
        """
        self.__excel_files = excel_files

if __name__ == "__main__":
    csv_masher = CSVMasher()
    print(csv_masher.get_excel_files())
    csv_masher.create_csv_file()

Publish

I made the contents, so I released it! !! I want to go there, There are things to think about when making it into an exe or publishing it.

1. Check the library license

Since it is an exe this time, it will import the package. In that case, the license of the library will be "enclosed". You need to check the license because it is a different method from normal "use". (For those who know an easy method around here)

1-1. Collect package licenses

The packages used this time including the dependencies are as follows It's only importing one library, but considering the dependencies, I think it will increase to the Imozuru formula ..

Enclose the LICENSE file with the 1-2.exe file

Basically, if you enclose it, the copyright notice and license statement are required, so I think that openpyxl and its dependent et-xmlfile and jdcal should be copyrighted. The license statement will be quite long, but I think it's better than unintentionally hurting the author without displaying it.

reference

https://qastack.jp/software/121998/mit-vs-bsd-vs-dual-license

https://mozxxx.hatenadiary.org/entry/20110529/p4

https://www.catch.jp/oss-license/2013/09/27/mit_license/

2. Think about how to release the exe file

There are several ways, This time, I'd like to study with the release function of GitHub.

This article is easy to understand how to use, so it is recommended.

https://qiita.com/keita69sawada/items/da6d8f6b6fb8f05ca670

3. Actually publish

The release is completed successfully. image.png

4. Also publish the GitHub Repository

Open Repository Settings image.png

Scroll down and it's kind of scary, but it's done with Make public in the Danger Zone column. image.png

Summary

It was harder than I expected. .. Especially around the license, it does not matter when importing and using it normally, It seems that you need to be careful when converting to exe.

Recommended Posts

From Excel file to exe and release of tool that spits out CSV
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
[Notes / Updated from time to time] This and that of Azure Functions
Normalize the file that converted Excel to csv as it is.
Python script that outputs all records of Oracle table to CSV file
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Import Excel file from Python (register to DB)
Created a tool to output a sequence diagram from a packet capture file of multiple nodes
Procedure to exe python file from Ubunts environment construction
How to convert Json file to CSV format or EXCEL format
Python script to create a JSON file from a CSV file
[Python] Creating a GUI tool that automatically processes CSV of temperature rise data in Excel
Precautions when inputting from CSV with Python and outputting to json to make it an exe
Now "average of sums from 1 to 10" and its execution speed
[Python] About creating a tool to create a new Outlook email based on the data of the JSON file and the part that got caught