user21359
user21359

Reputation: 476

Python-Docx missing default template

I recently installed the python-docx package and I'm having some trouble with the default template. I'm just setting up a document and have the following code (in a file called maintitle.py):

from docx import Document
from docx.shared import Inches

document = Document()

However, I get the following error:

Traceback (most recent call last):
  File "/Users/myname/Desktop/Python/maintitle.py", line 4, in <module>
    document = Document()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/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 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/templates/default-docx-template'

Taking a look in the directory "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/templates" there are only four files:

default-footer.xml  default-settings.xml
default-header.xml  default-styles.xml

I installed python-docx with pip3 install python-docx. Should I do something else?

Upvotes: 3

Views: 1688

Answers (3)

The Student
The Student

Reputation: 1

To resolve the issue in python-docx==1.1.2, simply follow these steps:

  1. Locate the default_sanitized.docx file: C:\Users\<YourUsername>\anaconda3\Lib\site-packages\docx\templates

  2. Copy and rename the file:

    • Make a copy of default_sanitized.docx
    • Rename the copied file to default.docx

Upvotes: 0

YC Chan
YC Chan

Reputation: 51

I have the same problem, but after using the below command everything looks good.

Use:

pyinstaller --collect-data "docxcompose" --onefile d:\python\Merge_Docx.py

instead of:

pyinstaller --onefile d:\python\Merge_Docx.py

(d:\python\Merge_Docx.py is the python script located)

Upvotes: 0

scanny
scanny

Reputation: 28893

There was a problem in release v0.8.9 that raised this error in certain environments. If you install v0.8.10 this should go away.

$ pip install python-docx==0.8.10

In some Ubuntu and possibly Mint distributions having an older setuptools version, you may also need to update setuptools:

$ pip install -U setuptools

For whatever reason, those distributions have a v20.x setuptools where the latest is v40.x.

Upvotes: 4

Related Questions