cx_freeze creates exe but does not save data in docx or xlsx

0

I have written a simple application with PyQt4 that performs some calculations. With four pushButtons I save the data, I recover it, I keep it in docx and xlsx format for the project documentation and that a third party can access them.

To create the files docx and xlsx use python-docx and XlsxWriter , for this I create some modules in the folder scripts .

The structure of the project is as follows:

classes/  # Clases comunes con otros proyectos.
    |
    |
miproyecto/
    |--__init__.py
    |--miproyecto.pyw
    |--setup.py  # Convierte a exe con cx_Freeze.
    |
    |--icon/  # Iconos del proyecto.
    |
    |--qtdesigner/
    |    |--__init__.py
    |    |--miproyecto.ui
    |    |--miproyecto.py
    |
    |--scripts/
         |--__init__.py
         |--py2docx.py  # Exporta datos a docx.
         |--py2xlsx.py  # Exporta datos a xlsx.

Within miproyecto.pyw I call the different modules in the following way:

from classes.FEM_2131_2132.chapter_2.loads_0_0_170808 import AdditionalLoads
from qtdesigner.miproyecto import *
from scripts.py2xlsx import py2xlsx
from scripts.py2docx import py2docx

py2xlsx and py2docx are functions defined within py2xlsx.py and py2docx.py that create the corresponding files.

The docx and xlsx files are saved in miproyecto.pyw as follows:

    # Export to docx.

    def export_docx(self):    
        name_docx = QtGui.QFileDialog.getSaveFileName(self, 'Save docx', '', '*.docx;; All Files (*.*)')    
        name_py = os.path.basename(__file__)[:-4]    
        data = self.data()    
        py2docx(name_docx, name_py, *data)

    # Export to xlsx.

    def export_xlsx(self):    
        name_xlsx = QtGui.QFileDialog.getSaveFileName(self, 'Save xlsx', '', '*.xlsx;; All Files (*.*)')    
        name_py = os.path.basename(__file__)[:-4]    
        data = self.data()    
        data_temp1 = [d.split('\n') for d in data]  # Get rid of '\n' between fields.
        data_temp2 = []
        for d in data_temp1:
            data_temp2 = data_temp2 + d
        data = data_temp2    
        py2xlsx(name_xlsx, name_py, *data)

The setup.py script contains the following:

from cx_Freeze import setup, Executable

excludes = ['tkinter']
packages = ['os', 'docx', 'lxml']

build_exe_options = {
    'excludes': excludes,
    'packages': packages
    }

exe = Executable(
    script='miproyecto.pyw',
    base='Win32GUI',
    targetName='miproyecto.exe',
    icon='.\icon\Air.ico'
    )

setup(
    name='miproyecto',
    version='0',
    description='Wind action according to FEM 2131/2132',
    options={'build_exe': build_exe_options},
    executables=[exe]
    )

When I run Python the application works perfectly.

When I run the setup using python setup.py build it creates the folder build with the executable. When I launch it, the program works, calculates the data, saves it, reads it again, when I give the buttons to save in docx or xlsx, the save window opens, but the files are not saved.

Even if you enter the name and press save, nothing is saved.

As when executing Python everything works and when creating the executable with cx_Freeze it does not give an error message and the folder with the files is created, apparently all normal, I do not know how to attack this problem.

Edited on 09-21-2017

Following the advice of @FJSevilla I have introduced a logging in the export_docx() method:

def export_docx(self):

    logging.debug('Abre ventana para guardar docx.')
    name_docx = QtGui.QFileDialog.getSaveFileName(self, 'Save docx', '', '*.docx;; All Files (*.*)')

    logging.debug('Registra nombre de la aplicación.')
    name_py = os.path.basename(__file__)[:-4]

    logging.debug('Traspasa datos.')
    data = self.data()

    logging.debug('Ejecuta la función py2docx() y guarda fichero docx.')
    py2docx(name_docx, name_py, *data)

When I run the program from the Python terminal I get the following log:

2017-09-21 18:47:43,660 : DEBUG : Abre ventana para guardar docx.
2017-09-21 18:47:51,189 : DEBUG : Registra nombre de la aplicación.
2017-09-21 18:47:51,189 : DEBUG : Traspasa datos.
2017-09-21 18:47:51,189 : DEBUG : Ejecuta la función py2docx() y guarda fichero docx.

But when launching the setup of cx_Frezze I get the following:

2017-09-21 18:49:44,306 : DEBUG : Abre ventana para guardar docx.
2017-09-21 18:49:52,942 : DEBUG : Registra nombre de la aplicación.
    
asked by Pedro Biel 20.09.2017 в 19:42
source

1 answer

0

After doing the next logging more debug in the method export_doc

def export_docx(self):

    logging.info('Abre ventana para guardar docx.')
    name_docx = QtGui.QFileDialog.getSaveFileName(self, 'Save docx', '', '*.docx;; All Files (*.*)')
    logging.info('Abierta ventana para guardar docx.')

    logging.info('Registra nombre de la aplicación.')
    name_py = os.path.basename(__file__)[:-4]
    logging.info('Registrado nombre de la aplicación.')

    logging.info('Traspasa datos.')
    data = self.data()
    logging.info('Traspasados datos.')

    logging.info('Ejecuta la función py2docx()')
    py2docx(name_docx, name_py, *data)
    logging.info('Ejecutada la función py2docx()')

I got the following result:

2017-09-30 18:51:23,811 : INFO : Abre ventana para guardar docx.
2017-09-30 18:51:30,038 : INFO : Abierta ventana para guardar docx.
2017-09-30 18:51:30,039 : INFO : Registra nombre de la aplicación.

Therefore the problematic code was:

name_py = os.path.basename(__file__)[:-4]

that pretends that the name of the application without the extension appears in the docx document. This track took me to the next link . When replacing it with the following code the problem has been solved.

    if getattr(sys, 'frozen', False):
        # frozen
        name_py = os.path.basename(sys.executable)[:-4]
    else:
        # unfrozen
        name_py = os.path.basename(os.path.realpath(__file__))[:-4]
    
answered by 30.09.2017 в 19:05