【问题标题】:pyinstaller --onefile generates 0KB exepyinstaller --onefile 生成0KB exe
【发布时间】:2020-12-17 08:30:50
【问题描述】:

我编写了一个 python 脚本,它将文件夹中存在的所有 pdf 转换为 docx(在 wincom 包(pywin32)的帮助下)。如果我只在我的 windows10 x64 机器上运行 python 脚本,脚本可以正常工作,但是当我通过“pyinstaller --onefile pdf2docx.py”生成 exe 时,它​​最初会生成一个大小(11MB)的 exe,但是一旦 exe 完全生成它变成 0 KB。

这是我的代码

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()

我更感兴趣的是了解根本原因而不是解决方案。请帮助我并提前致谢。 :)

【问题讨论】:

    标签: python-3.x windows-10 pyinstaller


    【解决方案1】:

    我发现了问题。

    有一个企业应用程序(语义端点保护)阻止 pyinstaller 生成 exe 文件并将其视为有害软件。

    在就此事件联系相关团队后,现在它成功生成了exe并且运行良好。

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2017-05-21
      • 2013-05-18
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多