简体   繁体   中英

How to Pass dynamically create Powerpoint file to Google Cloud Storage in python

I'm trying to build a power-point in my python 2.7 application and upload it on the fly to Google Cloud Storage.

I can create the ppt, store on my local hard drive as an intermediate step and then pick up from there to upload to the Google cloud storage. This works well. However, my production application will run on Google App Server so I want to be able to create the powerpoint and upload to Google Storage directly (without the intermediate step).

Any ideas of how to do this? The blob.upload_from_file() seems to only be able to pickup files that are physically stored somewhere but as my app is building these powerpoints I don't know what to pass to the blob.upload_from_file as an argument? I tried to use StringIO module but its generating the error message below.

from google.cloud import storage
from pptx import Presentation
from StringIO import StringIO

prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

out_file = StringIO()
prs.save(out_file)

client = storage.Client()

bucket = client.get_bucket([GCP_Bucket_Name])

blob = bucket.blob('test.pptx')
blob.upload_from_file(out_file)
print blob2.public_url

ValueError: Stream must be at beginning.

You are uploading the out_file object, which is just an empty StringIO object.

Instead you have to:

  1. Save the Presentation object to the file system prs.save(savingPath)
  2. Read the object and put it in a StringIO object.
  3. Finally upload the the StringIO object.

Here is the code:

from google.cloud import storage
from pptx import Presentation
from StringIO import StringIO

#Creates presentation
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

prs.save('test.pptx')

client = storage.Client()

bucket = client.get_bucket('yourBucket')
blob = bucket.blob('test.pptx')

with open('test.pptx','r') as ppt:
    out_file = StringIO(ppt.read())

blob.upload_from_file(out_file)
print blob.public_url


Finally you mentioned the code will run on Google App Server, this product does not exist. If you meant Google App Engine keep in mind that not all the runtimes have access to the file system . So you might not be able to save the pptx file to the file system of the application.

I solved this problem by moving my GAE application from Standard to the Flexible environment in GAE. The flexible environment allows write to the preso file to the root directory and I pick it up from there, upload to Google storage and then delete the file in the root. This worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM