1

I have built a windows application for comparing two Excel sheets using Pandas dataframe. I used dataframe.stlye.apply to highlight some columns with different colors based on data and exporting it into a new Excel file. Also, I have used pyinstaller for converting .py into .exe.

code snippet - save_op_file function is for applying style and save into excel.

def save_op_file(df):
    save_path = filedialog.asksaveasfilename(defaultextension='xlsx')
    writer = pd.ExcelWriter(save_path, engine='xlsxwriter')

    df1 = ''
    item_count = 0
    for items in changes:
        pos_count = 0
        for pos in items:
            if item_count == 0 and pos_count == 0:
                df1 = df.style.apply(write_style_yellow, row_idx=pos[0], col_idx=pos[1], axis=None)
            else:
                if pos_count == 0:
                    df1 = df1.apply(write_style_yellow, row_idx=pos[0], col_idx=pos[1], axis= None)
                else:
                    df1 = df1.apply(write_style_red, row_idx=pos[0], col_idx=pos[1], axis= None)

            item_count += 1
            pos_count += 1
            print('df1:')
            print(df1)
            df1.to_excel(writer, sheet_name='Sheet1', index=False)
    writer.save()
def write_style_yellow(x, row_idx, col_idx):
    color = 'background-color: yellow'
    df_styler = pd.DataFrame('', index=x.index, columns=x.columns)
    df_styler.iloc[row_idx,col_idx] = color
    return df_styler
def write_style_red(x,row_idx,col_idx):
    color = 'background-color: red'
    df_styler = pd.DataFrame('', index=x.index, columns=x.columns)
    df_styler.iloc[row_idx,col_idx] = color
    return df_styler

Now, when I am running my application, it is throwing this error -

    Exception in Tkinter callback 
       Traceback (most recent call last): 
         File "tkinter\ init .py", line 1883, in _call_ 
         File "Excel.py", line 199, in <lambda> 
         File "Excel.py", line 217, in save_op_file 
         File "pandas\core\frame.py", line 836, in style 
         File "<frozen importlib._bootstrap>", line 991, in _find_and_load 
         File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked 
         File "<frozen importlib._bootstrap>", line 671, in _load_unlocked 
         File "c:\users\asus\pycharmprojects\myproject\venv\lib\site- 
        packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module 
          exec(bytecode, module. dict ) 
         File "pandas\io\formats\style.py", line 51, in <module> 
         File "pandas\io\formats\style.py", line 124, in Styler 
         File "jinja2\environment.py", line 883, in get_template 
         File "jinja2\environment.py", line 857, in _load_template 
         File "jinja2\loaders.py", line 115, in load File "jinja2\loaders.py", line 248, in get_source 
         File "pkg_resources\ init .py", line 1404, in has_resource 
         File "pkg_resources\ init .py", line 1473, in _has 
         NotlmplementedError: Can't perform this operation for unregistered loader type*

Could you please help me in resolving this issue. Thanks in advance.

1 Answers1

0

I am able to resolve this.

This issue is occuring because pkg_resources is not compatible with pyinstaller and jinja2 is using pkg_resources.

Resolution:

I used cx_Freeze instead of pyinstaller for converting .py file into .exe, and it worked fine for me. You can refer this for cx_Freeze- How can I convert a .py to .exe for Python?

Reference to use cx_Freeze -

How can I convert a .py to .exe for Python?

Thanks!

  • I have used `cx_Freeze` convert `main_app.py` python file which also import another python file named `data_check.py` including `tkinter`, `pandas`, etc. packages, to an `exe` file, it's able to open `tkinter` but not able to apply data manipulation function. – ah bon Dec 01 '20 at 00:47