【问题标题】:Create blobstore file from PdfFileWriter从 PdfFileWriter 创建 blobstore 文件
【发布时间】:2012-10-28 12:49:32
【问题描述】:

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

这是我的代码:

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)

【问题讨论】:

    标签: python file google-app-engine blobstore pypdf


    【解决方案1】:

    您可以尝试将数据输出到字符串缓冲区,然后将缓冲区写入 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。我试过了,还是不行……
    • “不工作”意味着你得到一个错误或什么?它到底是怎么不工作的?
    • 这意味着这个错误:WrongOpenModeError: File is opens for write。 # 打开文件并使用 files.open(file_name, 'a') as f: f.write('data') #这是谷歌示例,所以我认为我应该做类似的事情..
    • @Anna 查看更新,它包含代码 -- 不能作为评论发布。
    • 非常感谢!!那是我一直在寻找的。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多