0

I am a beginner in Python 3 and was trying out this program to read a file. The word files (.docx) is saved in the same directory as the program. I am getting the following error:

Traceback (most recent call last):
  File "C:/Users/satya/PycharmProjects/pythonProject/Readingfile.py", line 2, in <module>
    print(review.readline())
  File "H:\Anaconda\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 637: character maps to <undefined>
review = open("ANOTHER ROUND.docx", 'r')  
print(review.readline())
review.close()
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • `.docx` files are basically ZIP files containing several XML files. You cannot just do `open(...)` then `.readline()` to get at the contents. –  Jul 13 '21 at 18:43
  • 1
    You may want to use a 3rd-party package such as [python-docx](https://python-docx.readthedocs.io/en/latest/). –  Jul 13 '21 at 18:45
  • @PM77-1 Yes it answered my question. Apparently, there is no simple way to just read from a .docx file. It can only be done by importing the docx package. I also tried by simply converting the docx file to .txt and it worked. – Satyam Anand Jul 13 '21 at 19:01

0 Answers0