The console keeps telling me this, saying there is a parameter missing.
"D:\Program Files (x86)\Python\python.exe" D:/workspace/Glossary_Builder_Python/main.py
Please input the file path
C:\Users\Administrator\Desktop\Allergies.docx
Traceback (most recent call last):
File "D:/workspace/Glossary_Builder_Python/main.py", line 102, in <module>
main(sys.argv)
File "D:/workspace/Glossary_Builder_Python/main.py", line 98, in main
extractWdFrmDocx(filepath)
File "D:/workspace/Glossary_Builder_Python/main.py", line 18, in extractWdFrmDocx
document = Document(file)
TypeError: __init__() missing 1 required positional argument: 'part'
I am trying to extract some highlighted(in yellow) text from a docx file, using python-docx and python 3.7. When I go into Document func, the __init__
looks like this:
def __init__(self, element, part):
super(Document, self).__init__(element)
self._part = part
self.__body = None
so here, what is 'part' for?
Below is the extracting function and main function:
def extractWdFrmDocx(filepath):
# self.filepath = filepath
document = Document(filepath)
for para in document.paragraphs:
for run in para.runs:
if run.font.highlight_color == WD_COLOR_INDEX.YELLOW:
keyText.append(run.text)
print(keyText)
def main(argv):
print("Please input the file path")
filepath = input()
extractWdFrmDocx(filepath)
if __name__ == "__main__":
main(sys.argv)
Probably you imported Document
from docx.document
. You are not supposed to construct such a Document
object directly. Instead a function for creating Document
objects is provided as docx.Document
which expects one argument, the way you are using it right now.
Therefore your code should be:
import docx
[...]
document = docx.Document(filepath)
From documentation of python-docx:
Document objects
class docx.document.Document
[...]
Not intended to be constructed directly. Use docx.Document() to open or create a document.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.