cx_freeze and docx - problems when freezing
<p>I have a simple program that takes input from the user and then does scraping with selenium. Since the user doesn't have Python environment installed I would like to convert it to *.exe. I usually use cx_freeze for that and I have successfully converted .py programs to .exe. At first it was missing some modules (like lxml) but I was able to solve it.  Now I think I only have problem with docx package.</p>

<p>This is how I initiate the new document in my program (I guess this is what causes me problems):</p>

<pre><code>doc = Document()
#then I do some stuff to it and add paragraph and in the end...
doc.save('results.docx')
</code></pre>

<p>When I run it from python everything works fine but when I convert to exe I get this error:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in &lt;module&gt;

    exec(code, m.__dict__)
  File "tribunalRio.py", line 30, in &lt;module&gt;
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
    "Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\library.zip\docx\templates\default.docx'
</code></pre>

<p>This is my setup.py program:</p>

<pre><code>from cx_Freeze import setup, Executable

executable = Executable( script = "tribunalRio.py" )

# Add certificate to the build
options = {
    "build_exe": {'include_files' : ['default.docx'],
        'packages' : ["lxml._elementpath", "inspect", "docx", "selenium"]
    }
}

setup(
    version = "0",
    requires = [],
    options = options,
    executables = [executable])
</code></pre>

<p>I thought that explicitly adding default.docx to the package would solve the problem (I have even tried adding it to the library.zip but it gives me even more errors) but it didn't. I have seen this <a href="http://stackoverflow.com/questions/30543169/cx-freeze-exe-failing-to-completely-import-docx">post</a> but I don't know what they mean by:</p>

<blockquote>
  <p>copying the docx document.py module inside my function (instead of
  using Document()</p>
</blockquote>

<p>Any ideas? I know that freezing is not the best solution but I really don't want to build a web interface for such a simple program...</p>

<p>EDIT:</p>

<p>I have just tried <a href="http://cx-freeze.readthedocs.io/en/latest/faq.html#using-data-files" rel="nofollow">this solution</a> :</p>

<pre><code>def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

doc = Document(find_data_file('default.docx'))
</code></pre>

<p>but again receive Traceback error (but the file is in this location...):</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in &lt;module&gt;

    exec(code, m.__dict__)
  File "tribunalRio.py", line 43, in &lt;module&gt;
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
    "Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\default.docx'
</code></pre>

<p>What am I doing wrong?</p>

<p>I expect you'll find the problem has to do with your freezing operation not placing the default Document() template in the expected location. It's stored as package data in the python-docx package as <code>docx/templates/default.docx</code> (see setup.py here: <a href="https://github.com/python-openxml/python-docx/blob/master/setup.py#L37" rel="nofollow">https://github.com/python-openxml/python-docx/blob/master/setup.py#L37</a>)</p>

<p>I don't know how to fix that in your case, but that's where the problem is it looks like.</p>

python python-3.x cx-freeze python-docx