How to save a presentation to a file-like object in Python 3
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:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpresentation.py", line 46, in save
self.part.save(file)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpartspresentation.py", line 118, in save
self.package.save(path_or_stream)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpackage.py", line 166, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 47, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, content_types_blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcphys_pkg.py", line 156, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1645, in writestr
with self.open(zinfo, mode='w') as dest:
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1349, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.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?
python python-pptx
add a comment |
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:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpresentation.py", line 46, in save
self.part.save(file)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpartspresentation.py", line 118, in save
self.package.save(path_or_stream)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpackage.py", line 166, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 47, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, content_types_blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcphys_pkg.py", line 156, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1645, in writestr
with self.open(zinfo, mode='w') as dest:
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1349, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.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?
python python-pptx
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
I guesspresentation.save
calls its argument’s.write
method. In your caseStringIO#write
accepts strings and not bytes, hence the error. UsingBytesIO
is the way to go.
– bfontaine
Oct 27 '17 at 16:39
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53
add a comment |
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:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpresentation.py", line 46, in save
self.part.save(file)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpartspresentation.py", line 118, in save
self.package.save(path_or_stream)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpackage.py", line 166, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 47, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, content_types_blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcphys_pkg.py", line 156, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1645, in writestr
with self.open(zinfo, mode='w') as dest:
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1349, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.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?
python python-pptx
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:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpresentation.py", line 46, in save
self.part.save(file)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxpartspresentation.py", line 118, in save
self.package.save(path_or_stream)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpackage.py", line 166, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcpkgwriter.py", line 47, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, content_types_blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibsite-packagespptxopcphys_pkg.py", line 156, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1645, in writestr
with self.open(zinfo, mode='w') as dest:
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.py", line 1349, in open
return self._open_to_write(zinfo, force_zip64=force_zip64)
File "C:UsersmgplanteAppDataLocalContinuumAnaconda2envsppt_genlibzipfile.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?
python python-pptx
python python-pptx
edited Oct 27 '17 at 16:52
asked Oct 27 '17 at 16:17
MichaelPlante
182110
182110
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
I guesspresentation.save
calls its argument’s.write
method. In your caseStringIO#write
accepts strings and not bytes, hence the error. UsingBytesIO
is the way to go.
– bfontaine
Oct 27 '17 at 16:39
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53
add a comment |
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
I guesspresentation.save
calls its argument’s.write
method. In your caseStringIO#write
accepts strings and not bytes, hence the error. UsingBytesIO
is the way to go.
– bfontaine
Oct 27 '17 at 16:39
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
I guess
presentation.save
calls its argument’s .write
method. In your case StringIO#write
accepts strings and not bytes, hence the error. Using BytesIO
is the way to go.– bfontaine
Oct 27 '17 at 16:39
I guess
presentation.save
calls its argument’s .write
method. In your case StringIO#write
accepts strings and not bytes, hence the error. Using BytesIO
is the way to go.– bfontaine
Oct 27 '17 at 16:39
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53
add a comment |
3 Answers
3
active
oldest
votes
How about BytesIO()
?
from pptx import Presentation
from io import BytesIO
presentation = Presentation('presentation.pptx')
output = BytesIO()
presentation.save(output)
This removes the error at least.
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
add a comment |
Hannu's answer is quite right, and is precisely the code that is used to verify this behavior in the test suite for python-pptx
:
stream = BytesIO()
presentation.save(stream)
https://github.com/scanny/python-pptx/blob/master/features/steps/presentation.py#L105
If that code is giving you a blank presentation, then something else is going on. I would reproduce that behavior, get it stable and repeatable, then ask the question to the effect "Why am I getting a blank presentation?" in another SO question, posting with it the full minimum code that gives you that behavior.
This is the second time I've heard of something like this, which makes me suspect there's actually something half-way systematic happening under the covers to produce this behavior. But at the same time, it's extremely unlikely that you would end up with a fully working presentation, just empty of slides, as a partial failure of attempting a save to a stream.
A common situation that could lead to this is saving a newly-opened default presentation, like:
prs = Presentation()
output = BytesIO()
prs.save(output)
This of course is not something you would likely do on purpose, but easy enough to do by accident, so I thought I'd mention.
If you can help us repeat your result we'll get it figured out :)
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
add a comment |
I faced the same problem while developing CGI-like presentation. My pptx shoud be sent as a file to user. You can send pptx as a file using BytesIO, not StringIO
qs = cgi.FieldStorage()
link = qs.getfirst('link', 'default_link_for_debug')
...
...
target_stream = BytesIO()
prs.save(target_stream)
target_stream.seek(0) # important!
length = target_stream.getbuffer().nbytes
buffer = target_stream.getbuffer()
#buffer = target_stream.read() # it also works
if 'HTTP_HOST' in os.environ:
sys.stdout.buffer.write(b'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentationrn')
sys.stdout.buffer.write('Content-Disposition: attachment; filename="offer-{0}.pptx"rn'.format(link).encode('ascii'))
sys.stdout.buffer.write('Content-Length: {0}rn'.format(length).encode('ascii'))
sys.stdout.buffer.write(b'Pragma: no-cachern')
sys.stdout.buffer.write(b'rn')
sys.stdout.buffer.write(buffer)
else: # for debug
with open("offer-{0}.pptx".format(link),'wb') as out:
out.write(buffer)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46979457%2fhow-to-save-a-presentation-to-a-file-like-object-in-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about BytesIO()
?
from pptx import Presentation
from io import BytesIO
presentation = Presentation('presentation.pptx')
output = BytesIO()
presentation.save(output)
This removes the error at least.
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
add a comment |
How about BytesIO()
?
from pptx import Presentation
from io import BytesIO
presentation = Presentation('presentation.pptx')
output = BytesIO()
presentation.save(output)
This removes the error at least.
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
add a comment |
How about BytesIO()
?
from pptx import Presentation
from io import BytesIO
presentation = Presentation('presentation.pptx')
output = BytesIO()
presentation.save(output)
This removes the error at least.
How about BytesIO()
?
from pptx import Presentation
from io import BytesIO
presentation = Presentation('presentation.pptx')
output = BytesIO()
presentation.save(output)
This removes the error at least.
answered Oct 27 '17 at 16:35
Hannu
5,12031730
5,12031730
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
add a comment |
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
That was my first instinct. Using BytesIO gave me a blank powerpoint file.
– MichaelPlante
Oct 27 '17 at 16:50
add a comment |
Hannu's answer is quite right, and is precisely the code that is used to verify this behavior in the test suite for python-pptx
:
stream = BytesIO()
presentation.save(stream)
https://github.com/scanny/python-pptx/blob/master/features/steps/presentation.py#L105
If that code is giving you a blank presentation, then something else is going on. I would reproduce that behavior, get it stable and repeatable, then ask the question to the effect "Why am I getting a blank presentation?" in another SO question, posting with it the full minimum code that gives you that behavior.
This is the second time I've heard of something like this, which makes me suspect there's actually something half-way systematic happening under the covers to produce this behavior. But at the same time, it's extremely unlikely that you would end up with a fully working presentation, just empty of slides, as a partial failure of attempting a save to a stream.
A common situation that could lead to this is saving a newly-opened default presentation, like:
prs = Presentation()
output = BytesIO()
prs.save(output)
This of course is not something you would likely do on purpose, but easy enough to do by accident, so I thought I'd mention.
If you can help us repeat your result we'll get it figured out :)
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
add a comment |
Hannu's answer is quite right, and is precisely the code that is used to verify this behavior in the test suite for python-pptx
:
stream = BytesIO()
presentation.save(stream)
https://github.com/scanny/python-pptx/blob/master/features/steps/presentation.py#L105
If that code is giving you a blank presentation, then something else is going on. I would reproduce that behavior, get it stable and repeatable, then ask the question to the effect "Why am I getting a blank presentation?" in another SO question, posting with it the full minimum code that gives you that behavior.
This is the second time I've heard of something like this, which makes me suspect there's actually something half-way systematic happening under the covers to produce this behavior. But at the same time, it's extremely unlikely that you would end up with a fully working presentation, just empty of slides, as a partial failure of attempting a save to a stream.
A common situation that could lead to this is saving a newly-opened default presentation, like:
prs = Presentation()
output = BytesIO()
prs.save(output)
This of course is not something you would likely do on purpose, but easy enough to do by accident, so I thought I'd mention.
If you can help us repeat your result we'll get it figured out :)
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
add a comment |
Hannu's answer is quite right, and is precisely the code that is used to verify this behavior in the test suite for python-pptx
:
stream = BytesIO()
presentation.save(stream)
https://github.com/scanny/python-pptx/blob/master/features/steps/presentation.py#L105
If that code is giving you a blank presentation, then something else is going on. I would reproduce that behavior, get it stable and repeatable, then ask the question to the effect "Why am I getting a blank presentation?" in another SO question, posting with it the full minimum code that gives you that behavior.
This is the second time I've heard of something like this, which makes me suspect there's actually something half-way systematic happening under the covers to produce this behavior. But at the same time, it's extremely unlikely that you would end up with a fully working presentation, just empty of slides, as a partial failure of attempting a save to a stream.
A common situation that could lead to this is saving a newly-opened default presentation, like:
prs = Presentation()
output = BytesIO()
prs.save(output)
This of course is not something you would likely do on purpose, but easy enough to do by accident, so I thought I'd mention.
If you can help us repeat your result we'll get it figured out :)
Hannu's answer is quite right, and is precisely the code that is used to verify this behavior in the test suite for python-pptx
:
stream = BytesIO()
presentation.save(stream)
https://github.com/scanny/python-pptx/blob/master/features/steps/presentation.py#L105
If that code is giving you a blank presentation, then something else is going on. I would reproduce that behavior, get it stable and repeatable, then ask the question to the effect "Why am I getting a blank presentation?" in another SO question, posting with it the full minimum code that gives you that behavior.
This is the second time I've heard of something like this, which makes me suspect there's actually something half-way systematic happening under the covers to produce this behavior. But at the same time, it's extremely unlikely that you would end up with a fully working presentation, just empty of slides, as a partial failure of attempting a save to a stream.
A common situation that could lead to this is saving a newly-opened default presentation, like:
prs = Presentation()
output = BytesIO()
prs.save(output)
This of course is not something you would likely do on purpose, but easy enough to do by accident, so I thought I'd mention.
If you can help us repeat your result we'll get it figured out :)
answered Oct 27 '17 at 17:46
scanny
9,61912042
9,61912042
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
add a comment |
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
Thanks. New question is up
– MichaelPlante
Oct 27 '17 at 18:42
add a comment |
I faced the same problem while developing CGI-like presentation. My pptx shoud be sent as a file to user. You can send pptx as a file using BytesIO, not StringIO
qs = cgi.FieldStorage()
link = qs.getfirst('link', 'default_link_for_debug')
...
...
target_stream = BytesIO()
prs.save(target_stream)
target_stream.seek(0) # important!
length = target_stream.getbuffer().nbytes
buffer = target_stream.getbuffer()
#buffer = target_stream.read() # it also works
if 'HTTP_HOST' in os.environ:
sys.stdout.buffer.write(b'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentationrn')
sys.stdout.buffer.write('Content-Disposition: attachment; filename="offer-{0}.pptx"rn'.format(link).encode('ascii'))
sys.stdout.buffer.write('Content-Length: {0}rn'.format(length).encode('ascii'))
sys.stdout.buffer.write(b'Pragma: no-cachern')
sys.stdout.buffer.write(b'rn')
sys.stdout.buffer.write(buffer)
else: # for debug
with open("offer-{0}.pptx".format(link),'wb') as out:
out.write(buffer)
add a comment |
I faced the same problem while developing CGI-like presentation. My pptx shoud be sent as a file to user. You can send pptx as a file using BytesIO, not StringIO
qs = cgi.FieldStorage()
link = qs.getfirst('link', 'default_link_for_debug')
...
...
target_stream = BytesIO()
prs.save(target_stream)
target_stream.seek(0) # important!
length = target_stream.getbuffer().nbytes
buffer = target_stream.getbuffer()
#buffer = target_stream.read() # it also works
if 'HTTP_HOST' in os.environ:
sys.stdout.buffer.write(b'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentationrn')
sys.stdout.buffer.write('Content-Disposition: attachment; filename="offer-{0}.pptx"rn'.format(link).encode('ascii'))
sys.stdout.buffer.write('Content-Length: {0}rn'.format(length).encode('ascii'))
sys.stdout.buffer.write(b'Pragma: no-cachern')
sys.stdout.buffer.write(b'rn')
sys.stdout.buffer.write(buffer)
else: # for debug
with open("offer-{0}.pptx".format(link),'wb') as out:
out.write(buffer)
add a comment |
I faced the same problem while developing CGI-like presentation. My pptx shoud be sent as a file to user. You can send pptx as a file using BytesIO, not StringIO
qs = cgi.FieldStorage()
link = qs.getfirst('link', 'default_link_for_debug')
...
...
target_stream = BytesIO()
prs.save(target_stream)
target_stream.seek(0) # important!
length = target_stream.getbuffer().nbytes
buffer = target_stream.getbuffer()
#buffer = target_stream.read() # it also works
if 'HTTP_HOST' in os.environ:
sys.stdout.buffer.write(b'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentationrn')
sys.stdout.buffer.write('Content-Disposition: attachment; filename="offer-{0}.pptx"rn'.format(link).encode('ascii'))
sys.stdout.buffer.write('Content-Length: {0}rn'.format(length).encode('ascii'))
sys.stdout.buffer.write(b'Pragma: no-cachern')
sys.stdout.buffer.write(b'rn')
sys.stdout.buffer.write(buffer)
else: # for debug
with open("offer-{0}.pptx".format(link),'wb') as out:
out.write(buffer)
I faced the same problem while developing CGI-like presentation. My pptx shoud be sent as a file to user. You can send pptx as a file using BytesIO, not StringIO
qs = cgi.FieldStorage()
link = qs.getfirst('link', 'default_link_for_debug')
...
...
target_stream = BytesIO()
prs.save(target_stream)
target_stream.seek(0) # important!
length = target_stream.getbuffer().nbytes
buffer = target_stream.getbuffer()
#buffer = target_stream.read() # it also works
if 'HTTP_HOST' in os.environ:
sys.stdout.buffer.write(b'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentationrn')
sys.stdout.buffer.write('Content-Disposition: attachment; filename="offer-{0}.pptx"rn'.format(link).encode('ascii'))
sys.stdout.buffer.write('Content-Length: {0}rn'.format(length).encode('ascii'))
sys.stdout.buffer.write(b'Pragma: no-cachern')
sys.stdout.buffer.write(b'rn')
sys.stdout.buffer.write(buffer)
else: # for debug
with open("offer-{0}.pptx".format(link),'wb') as out:
out.write(buffer)
answered Nov 23 '18 at 1:06
Павел П
313
313
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46979457%2fhow-to-save-a-presentation-to-a-file-like-object-in-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have the full stack trace?
– Patrick Haugh
Oct 27 '17 at 16:28
I guess
presentation.save
calls its argument’s.write
method. In your caseStringIO#write
accepts strings and not bytes, hence the error. UsingBytesIO
is the way to go.– bfontaine
Oct 27 '17 at 16:39
Edited in the full stack trace. I did try BytesIO, but it produced a blank powerpoint.
– MichaelPlante
Oct 27 '17 at 16:53