jw chen
jw chen

Reputation: 23

why my IDE can't Auto-Complete for python-docx?

when i use python-docx , my IDE ,like pycharm wing , can't Auto-Complete it. this code

from docx import Document

asd = Document()
asd.add_heading("test")
asd.save("cao.docx")

when i typing asd. add_heading can't Auto-Complete.

from docx.document import Document  

asd = Document()
asd.save()
this code can Auto-Complete
but atention

TypeError: init() missing 2 required positional arguments: 'element' and 'part'

I am sorry for my poor english

Upvotes: 2

Views: 780

Answers (4)

Satyendra
Satyendra

Reputation: 1705

As a matter of fact, docx.Document(...) is actually a function, which returns an object of the docx.document.Document class.
(Perhaps they should have named this method by following the naming convention, say something like docx.create_document(...))

Hence, you shall use both the following imports, in order to get the visibility of the contents of docx.document.Document class:

from docx import Document
from docx.document import Document

Upvotes: 2

sebwr
sebwr

Reputation: 375

For me only the following worked for PyCharm 2023.1:


from docx import Document as CreateDocument
from docx.document import Document

document: Document = CreateDocument()

Upvotes: 2

afar
afar

Reputation: 145

Use this workaround to have the autocomplete feature in your IDE and not get the TypeError: init() missing 2 required positional arguments: 'element' and 'part' :

from docx.document import Document
try:
    document = Document()
except TypeError:
    from docx import Document
    document = Document()

Upvotes: 0

scanny
scanny

Reputation: 28883

In the latter case, you are mistakenly importing docx.document.Document rather than docx.Document. The Document class in docx.document has a different call signature and in any case isn't the one you want :)

Upvotes: 1

Related Questions