Why can't Python open a pptx file?
Question:
I want Python to open a powerpont file.
code:
f=open(r'''C:UsersAsusOneDriveMáy tínhpythontest.pptx''')
print(f.read())
terminal:
Traceback (most recent call last):
File "c:UsersAsusOneDriveMáy tínhpythonhọc.py", line 16, in <module>
print(f.read())
^^^^^^^^
File "C:UsersAsusAppDataLocalProgramsPythonPython311Libencodingscp1258.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8a in position 588: character maps to <undefined>
Answers:
You can use the python-pptx
library, since you are working with PowerPoint files, here is an example.
The error implies the file you are trying to read is not a text file and cannot be decoded using the default encoding. You can use the pptx
library to fix this.
import pptx
# Open the PowerPoint file
ppt = pptx.Presentation(r'C:PathToPowerPointFile.pptx')
# Iterate over each slide and print the text content
for slide in ppt.slides:
for shape in slide.shapes:
if hasattr(shape, 'text'):
print(shape.text)
I want Python to open a powerpont file.
code:
f=open(r'''C:UsersAsusOneDriveMáy tínhpythontest.pptx''')
print(f.read())
terminal:
Traceback (most recent call last):
File "c:UsersAsusOneDriveMáy tínhpythonhọc.py", line 16, in <module>
print(f.read())
^^^^^^^^
File "C:UsersAsusAppDataLocalProgramsPythonPython311Libencodingscp1258.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8a in position 588: character maps to <undefined>
You can use the python-pptx
library, since you are working with PowerPoint files, here is an example.
The error implies the file you are trying to read is not a text file and cannot be decoded using the default encoding. You can use the pptx
library to fix this.
import pptx
# Open the PowerPoint file
ppt = pptx.Presentation(r'C:PathToPowerPointFile.pptx')
# Iterate over each slide and print the text content
for slide in ppt.slides:
for shape in slide.shapes:
if hasattr(shape, 'text'):
print(shape.text)