pyinstaller --onefile generates 0KB exe

Viewed 142

I have written a python script which converts all pdf present in a folder to docx (with the help of wincom package(pywin32)). The script works fine if I run the python script only in my windows10 x64 machine, but when I generate the exe through "pyinstaller --onefile pdf2docx.py" it generates an exe initially of some size(11MB) but once exe is completely generated it becomes 0 KB.

This is my code

import os, json, traceback, sys, shutil
import win32com.client
from datetime import datetime
from inspect import currentframe

# Global Variables
logfile = open("log.txt", 'a+')

# Returns Current line number (for Debugging purpose)
def get_linenumber():
    cf = currentframe()
    return "Line No: "+str(cf.f_back.f_lineno)

# Handels Error
def handel_error(line_no = None ,description = None, data = None):
    log(f"Ended with Error(logged in Error.txt): {datetime.now()}\n{'-'*80}\n")
    err = traceback.format_exc()
    err_path= f"Error.txt"
    with open(err_path,"a+") as f:
        f.write(f"\n{datetime.now()}\n{'-'*100}\nUnexpected error:\n{description} : {data} at {line_no}\n{err}\n{str(sys.exc_info())}\n")

#  Returns dictionary from json file
def read_config(config):
    with open(config, 'r') as f:
        return json.load(f)

# logs into log(txt format) file
def log(text, file = logfile):
    file.write(f"{text}\n")

# Converts All pdf files from PDF directory to Word file and place it in DOC directory (Directories are provided in the config.json file)
# If pop-up appears, check the checkbox Don't show again and press ok
def pdf2docx(pdf_dir,docx_dir):
    for fle in os.listdir(pdf_dir):
        pdf = os.path.join(pdf_dir, fle)
        name,ext = os.path.splitext(fle)
        if ext == '.pdf':
            try:
                log(f"Processing : {fle}")
                wb = word.Documents.Open(pdf)
                out_file = os.path.abspath(f"{docx_dir}\\{name}.docx")
                print("Processing: ",fle)
                wb.SaveAs2(out_file, FileFormat=16) # file format for docx
                log("success...")
                wb.Close()
            except:
                handel_error()
        else:
            log(f"Not a pdf file: {fle}")

if __name__ == "__main__":
    try:
        # Reading the configuration file
        base_dir = os.path.dirname(os.path.abspath(__file__))
        config = read_config(f"{base_dir}\\config.json")
        pdf_dir = config['pdfs']
        docx_dir = config['docx']
        
        log(f"Started: {datetime.now()}\n")

        # Converting pdf to word
        word = win32com.client.Dispatch("Word.Application")
        word.visible = 0
        
        pdf2docx(pdf_dir,docx_dir)

        word.Quit()

        log(f"\nEnded: {datetime.now()}\n{'-'*80}\n")
    except:
        handel_error()

enter image description here

I'm more interested in knowing the root cause than the solution. Please help me and Thanks in advance. :)

1 Answers

I figured out the issue.

There was an enterprise application (semantic endpoint protection) that was blocking the pyinstaller from generating the exe files and was treating it as harmful software.

After contacting the appropriate team about this incident, now it generates the exe successfully and runs fine.

Related