简体   繁体   English

"为什么我的 IDE 不能为 python-docx 自动完成?"

[英]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.当我使用 python-docx 时,我的 IDE 像 pycharmwing 一样,无法自动完成它。 this code这段代码

from docx import Document

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

In the latter case, you are mistakenly importing docx.document.Document rather than docx.Document . 在后一种情况下,您错误地导入了docx.document.Document而不是docx.Document The Document class in docx.document has a different call signature and in any case isn't the one you want :) docx.documentDocument类具有不同的调用签名,无论如何都不是您想要的:)

As a matter of fact, docx.Document(...) is actually a method, which returns an object of the docx.document.Document class. 实际上, docx.Document(...)实际上是一个方法,它返回docx.document.Document类的对象。
(Perhaps they should have named this method by following the naming convention, say something like docx.create_document(...)) (也许他们应该通过遵循命名约定来命名此方法,比如说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: 因此,为了获得docx.document.Document类的内容的可见性,应同时使用以下两种导入方式:

from docx import Document
from docx.document import Document

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'<\/code> :使用此解决方法在您的 IDE 中具有自动完成功能,并且不会出现TypeError: init() missing 2 required positional arguments: 'element' and 'part'<\/code> :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM