Python 3 replaced StringIO.StringIO
with io.StringIO
. I've been able to successfully save presentations using the former, but it doesn't appear to work for the latter.
from pptx import Presentation
from io import StringIO
presentation = Presentation('presentation.pptx')
output = StringIO()
presentation.save(output)
The above code produces:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\presentation.py", line 46, in save
self.part.save(file)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\parts\presentation.py", line 118, in save
self.package.save(path_or_stream)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\opc\package.py", line 166, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\opc\pkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\opc\pkgwriter.py", line 47, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, content_types_blob)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\site-packages\pptx\opc\phys_pkg.py", line 156, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\zipfile.py", line 1645, in writestr
with self.open(zinfo, mode='w') as dest:
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\zipfile.py", line 1349, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "C:\Users\mgplante\AppData\Local\Continuum\Anaconda2\envs\ppt_gen\lib\zipfile.py", line 1462, in _open_to_write
self.fp.write(zinfo.FileHeader(zip64))
TypeError: string argument expected, got 'bytes'
Is there a way to save a presentation to to a file-like object in Python 3, or am I going to have to use Python 2 for this project?