[Obj-C] 13 GUIアプリ : リストをxlsxファイル化

[M1 Mac, Big Sur 11.6.5, no Xcode]

Pythonのライブラリopenpyxlを使って、リストをxlsxファイルに変換できるようにしました。デスクトップにtest.xlsxとして作成されます。

コードは以下の通りです。

#include <Cocoa/Cocoa.h>
#include "process.h"
#include </Library/Frameworks/Python.framework/Versions/3.10/include/python3.10/Python.h>

@implementation Convert

- (NSString *)ConvertFunc:(NSString *)path number:(int)num {
    switch (num) {
    case 1:{
        Py_Initialize();
        // pyファイルの指定
        PyObject* myModuleString = PyUnicode_FromString((char*)"xlsx_convert");

        // pyファイルのモジュール化
        PyObject* myModule = PyImport_Import(myModuleString);

        // pyファイル内の関数を指定
        PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"xlsx_to_list");

        // 関数の引数を設定
        PyObject* args = PyTuple_Pack(1,PyUnicode_FromString((char *) [path UTF8String]));

        // 関数を実行し戻り値をPyObjectとして取得
        PyObject* myResult = PyObject_CallObject(myFunction,args);

        // PyObjectをconst char*に変換
        const char* result = PyUnicode_AsUTF8(myResult);
        NSString* result_ns = [NSString stringWithCString: result encoding:NSUTF8StringEncoding];

        NSLog(@"%@",result_ns);

        return result_ns;
        Py_FinalizeEx();
        break;
    }
    case 2:{
        Py_Initialize();

        PyObject* myModuleString = PyUnicode_FromString((char*)"xlsx_convert");
        PyObject* myModule = PyImport_Import(myModuleString);
        PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"xlsx_to_csv");
        PyObject* args = PyTuple_Pack(1,PyUnicode_FromString((char *) [path UTF8String]));
        PyObject* myResult = PyObject_CallObject(myFunction,args);
        const char* result = PyUnicode_AsUTF8(myResult);
        NSString* result_ns = [NSString stringWithCString: result encoding:NSUTF8StringEncoding];

        return result_ns;
        Py_FinalizeEx();
        break;
    }           
    case 3:{
        Py_Initialize();

        PyObject* myModuleString = PyUnicode_FromString((char*)"xlsx_convert");
        PyObject* myModule = PyImport_Import(myModuleString);
        PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"list_to_xlsx");
        PyObject* args = PyTuple_Pack(1,PyUnicode_FromString((char *) [path UTF8String]));
        PyObject* myResult = PyObject_CallObject(myFunction,args);
        const char* result = PyUnicode_AsUTF8(myResult);
        NSString* result_ns = [NSString stringWithCString: result encoding:NSUTF8StringEncoding];

        return result_ns;
        Py_FinalizeEx();
        break;
    }
    case 4:
        return @"case 4";
        break;
    case 5:
        return @"case 5";
        break;
    case 6:
        return @"case 6";
        break;
    default:
        return @"default";
        break;
    }

}
@end
import openpyxl, os
import pandas as pd

def xlsx_to_list(path):
    wb = openpyxl.load_workbook(path)
    ws = wb.worksheets[0]

    color_name = []
    color_code = []
    for cell in ws['A']:
        color_name.append(cell.value)
        color_code.append(cell.fill.fgColor.rgb)
        
    return str(color_name)

def xlsx_to_csv(path):
    # csvファイル名作成
    csv_path = path.split('.')[0] + '.csv'
    
    wb = openpyxl.load_workbook(path)
    ws = wb.worksheets[0]
    df = pd.DataFrame(ws.values)
    
    df.to_csv(csv_path, index = False, header = None)

    return "xlsx_to_csv success"
    
def list_to_xlsx(list):
    home = os.path.expanduser('~')
    file = home + '/Desktop/test.xlsx'
    
    wb = openpyxl.Workbook()
    ws = wb.worksheets[0]

    # 引数の文字列をリスト化
    new_list = eval(list)
    
    num = 0
    for i in range(1,1 +1):
        for j in range(1,len(new_list) +1):
            ws.cell(row = j, column = i).value = new_list[num]
            num = num + 1

    wb.save(file)
    
    return "list_to_xlsx success"