2012-11-08 35 views
2

創建Blob存儲文件,我想合併使用Python和谷歌應用程序兩款發動機的PDF與pyPdf庫。 我讀Blob存儲的文件和我創建,我需要後來我在煩惱這個PdfFileWriter轉換成一個Blob存儲文件中的信息PdfFileWriter對象。任何想法解決它? 謝謝:)從PdfFileWriter

這裏是我的代碼:

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) 

回答

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

然後敲定並執行通常的操作。

+0

謝謝你盡力幫助我,但是這條線是不正確的,因爲我在的f是作家裏面的信息來編寫。我試過了,它不起作用... – Anna

+0

「不起作用」是指你得到一個錯誤或什麼?它究竟如何不起作用? – lenik

+0

這意味着這個錯誤: WrongOpenModeError:打開文件進行寫操作。 #打開文件,並寫入它 與files.open(FILE_NAME,「A」)爲f: f.write(「數據」)#這是谷歌實例以便我想我應該做同樣的事情.. – Anna