如何使用pyinstaller创建的exe能够运行docxcompose和python-docs并访问.docs文件?

问题描述 投票:0回答:1

我已经制作了一个程序,该程序可以基于我使用pysimplegui从GUI获得的用户输入来创建docx文件。

我希望将另一个docx文件附加到从用户界面创建的文件中,但是我的exe文件无法执行。

当我在PyCharm中运行程序时它起作用,但是我的exe文件失败。我怀疑存在一些路径错误,但是我已经尝试了几乎所有可能是错误的组合,所以现在我怀疑它可能与python-docxcompose有关。

我的text_file_db包含许多我可以在exe文件中访问的av txt文件,但是当我尝试使用python-docxcompose函数时,它会失败。

目录概述

src/
    beta.py
    directory.py
    doc_write.py
    text_file_db/
        docs/
            document.docx

我的代码:

#! c:/user/src/beta.py


def beta():

    values = gui()
    create_doc(values)

    return 

[借助[Bundling data files with PyInstaller (--onefile))的Max答案


#! c:/user/src/doc_write.py

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except AttributeError:
        base_path = os.path.abspath(".")
    path = os.path.join(base_path, relative_path)
    return path


def create_doc(values):
    doc = Document()
    relative_path = os.path.join('text_file_db\\docs','document.docx'
    path = resource_path(relative_path)
    doc = combin_docs(doc, path)
...

#! c:/user/src/directory.py

from docxcompose.composer import Composer
from docx import Document as Document_compose

def combin_docs(doc, appending):
    alt_doc = Composer(doc)
    doc2 = Document_compose(appending)
    alt_doc.append(doc2)
    new_doc = alt_doc.doc
    return new_doc

要创建exe,我使用pyinstaller。


$ pyinstaller --windowed --noconsole --clean --onefile beta.py

#! c:/user/src/beta.spec

Adding:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          Tree('./text_file_db', prefix='text_file_db'),
....


$ pyinstaller --windowed --noconsole --clean --onefile beta.spec

´´´
python pyinstaller python-docx
1个回答
0
投票

我发现了一个相关的解决方案,可以潜在地帮助您解决问题。请参阅我的答案,以通过spec文件在以下线程中手动将docxcompose作为软件包包括在内:How can I add a Python site-package folder (that's not being included) to a PyInstaller spec file?

© www.soinside.com 2019 - 2024. All rights reserved.