AlJohri / docx2pdf

MIT License
497 stars 94 forks source link

docx2pdf 'NoneType' Error after packaging .exe #75

Closed ketchupkungen closed 1 year ago

ketchupkungen commented 1 year ago

I'm kinda new to this docx2pdf, and farily rookie in python.. Is anyone familiar with handling docx2pdf with packaging to .exe? I have a script that uses it, and it works just fine when run from commandline. Rewritten the code that implements it a couple of times in case it's something obvious I'm not seeing. But seems to be some difference when it comes to" path" between "regular" script, and .exe packaged script (excuse my caveman coding description).

packages I use this with is PySimpleGUI and exporting it to .exe with pyinstaller

An example verson of using it:

import docx2pdf
import os
import PySimpleGUI as sg

def convert_to_pdf():
    layout = [
        [sg.Text('Select .docx files to convert'), sg.Input(key='-FILES_IN-', enable_events=True, visible=False), sg.FilesBrowse(file_types=(("Word files", "*.docx"),), key='-FILES_IN-')],
        [sg.Text('Select folder to save .pdf files'), sg.Input(key='-FILES_OUT-', enable_events=True, visible=False), sg.FolderBrowse(key='-FILES_OUT-')],
        [sg.Button('Convert'), sg.Button('Cancel')]
    ]
  window = sg.Window('Convert .docx to .pdf', layout)

    while True:
        event, values = window.read()
        if event in (None, 'Cancel'):
            break
        if event == 'Convert':
            if values["-FILES_IN-"] != "" and values["-FILES_OUT-"] != "":
                files = values['-FILES_IN-'].split(';')
                folder = values['-FILES_OUT-']
                for file in files:
                    if file.endswith('.docx'):
                        pdf_file = os.path.join(folder, os.path.basename(file).replace('.docx', '.pdf'))
                        try:
                            docx2pdf.convert(file, pdf_file)
                            print(f'{file} has been converted to {pdf_file}')
                        except Exception as e:
                            sg.popup_no_titlebar("Conversion failed", f'Error: {e}')
                            break
                    else:
                        sg.popup_no_titlebar("This function is for converting .docx files to .pdf, did you select the wrong file?")
                sg.popup_no_titlebar("Conversion complete", f'The new pdf files are located at {folder}', 'You can now close the app')
                break
    window.close()

convert_to_pdf()

The error I keep getting when run from .exe Is "'NoneType' object has no attribute 'write'", upon execution.

Any clues or hints are greatly appreciated :)

ketchupkungen commented 1 year ago

Nevermind. I found that it was caused by the console was not included in the .exe export. I exported with --noconsole