Python 从PdfielWriter创建blobstore文件

Python 从PdfielWriter创建blobstore文件,python,file,google-app-engine,blobstore,pypdf,Python,File,Google App Engine,Blobstore,Pypdf,我正在尝试使用python和google app engine将两个PDF与pyPdf库合并。 我从blobstore读取文件,并使用所需的信息创建PdfielWriter对象,但在blobstore文件中转换此PdfielWriter时遇到问题。有办法解决吗? 谢谢:) 这是我的密码: blob_reader1 = blobstore.BlobReader(blob_key1) blob_reader2 = blobstore.BlobReader(blob_key2) writer = P

我正在尝试使用python和google app engine将两个PDF与pyPdf库合并。 我从blobstore读取文件,并使用所需的信息创建PdfielWriter对象,但在blobstore文件中转换此PdfielWriter时遇到问题。有办法解决吗? 谢谢:)

这是我的密码:

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)

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

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() )

然后完成并完成通常的工作。

感谢您尝试帮助我,但这一行是不正确的,因为我必须用f写作者内部的信息。我试过了,但没用…“没用”是指你出错了还是怎么了?它到底是如何工作的?这意味着这个错误:ErrorOpenModeError:文件被打开进行写入打开文件并用文件写入。以f:f.write('data')的形式打开(文件名,'a')。#这是谷歌的例子,所以我想我应该做类似的事情。@Anna see update,它包含代码--不能作为评论发布。非常感谢!!这就是我一直在寻找的。:)