N
N
NitroDesktop2020-05-23 09:10:47
Python
NitroDesktop, 2020-05-23 09:10:47

How to fix python and word executing constantly errors?

You need to place the text and the picture in a Word document, but no matter how you tried to run the example, all the time errors:

Traceback (most recent call last):
  File "d:\python\word.py", line 3, in <module>
    doc = docx.Document('example.docx')
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\docx\api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\docx\opc\package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\docx\opc\phys_pkg.py", line 30, in __new__
    raise PackageNotFoundError(
docx.opc.exceptions.PackageNotFoundError: Package not found at 'example.docx'


import docx

doc = docx.Document('example.docx')

# количество абзацев в документе
print(len(doc.paragraphs))

# текст первого абзаца в документе
print(doc.paragraphs[0].text)

# текст второго абзаца в документе
print(doc.paragraphs[1].text)

# текст первого Run второго абзаца
print(doc.paragraphs[1].runs[0].text)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2020-05-23
@tsklab

And the file you are trying to open - how was it created? It is important. If you created it from Explorer -> 'Create Microsoft Office Word Document', then in this case a PackageNotFoundError occurs.
The docx library does not accept them, because they are empty and there are no templates inside.

And, of course, also applies to the renamed .doc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question