-4

I would like Python to read my document, but then it gives an error. Do you know how I can solve this problem?

enter image description here

Traceback (most recent call last):
  File "/Users/PycharmProjects/Basis/venv/Python.py", line 3, in <module>
    print(document.read())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid continuation byte
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

1

You are trying to open a document with the extension .docx, which cannot be done with the open() function. Instead, you can try using the docx2txt library, as follows:

import docx2txt
my_text = docx2txt.process("test.docx")
print(my_text)
Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
0

please put full code so else can see what issue you facing

I am sharing the code for how to read or write any file in python

try to run code in python shell. may be your editor not configure properly

>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)      # Go to the 6th byte in the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2)  # Go to the 3rd byte before the end
13
>>> f.read(1)
b'd'

more reference you can refer to this one

https://python-docx.readthedocs.io/en/latest/user/documents.html

https://docs.python.org/3/tutorial/inputoutput.html

Mr Coder
  • 507
  • 3
  • 13