首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PdfFileWriter创建blobstore文件

从PdfFileWriter创建blobstore文件
EN

Stack Overflow用户
提问于 2012-11-08 18:22:49
回答 1查看 744关注 0票数 2

我正在尝试合并两个pdf与pyPdf库使用python和谷歌应用程序引擎。我从blobstore读取文件,并使用所需的信息创建PdfFileWriter对象,但是在将此PdfFileWriter转换为blobstore文件时遇到了麻烦。有什么办法解决这个问题吗?谢谢您:)

下面是我的代码:

代码语言:javascript
复制
blob_reader1 = blobstore.BlobReader(blob_key1)
blob_reader2 = blobstore.BlobReader(blob_key2)

writer = PdfFileWriter()

input1 = PdfFileReader(blob_reader1)
input2 = PdfFileReader(blob_reader2)

writer.addPage(input1.getPage(0))
writer.addPage(input1.getPage(1))
writer.addPage(input2.getPage(0))


# finally, write "output" to document-output.pdf
# This lines are comment because it is the way to do it without google app engine---
# outputStream = file("document-output.pdf", "wb")
# output.write(outputStream)
# outputStream.close()
#        
# Open the file and write to it
#I do not how write the information are inside writer in blobstore file
file_name = files.blobstore.create(mime_type='application/pdf')    
#with files.open(file_name, 'a') as f:
#    f.write(writer) it does not work

# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)

blob_key = files.blobstore.get_blob_key(file_name)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-08 19:02:53

您可以尝试将数据输出到字符串缓冲区,然后将缓冲区写入blob文件:

代码语言:javascript
复制
import StringIO

stream = StringIO.StringIO()
writer.write( stream )    # write PDF content

# Open the file and write to it
with files.open(file_name, 'a') as f:
    f.write( stream.getvalue() )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13287091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档