I am trying to render content of a word document file (docx) stored in google drive with Django templating. The word document file (docx) is the template with django variables. Converting the file to google docs format would let the docx file loose its font and style formatting hence i am trying to implement the following steps in google app engine
- Download the docx file using its downloadUrl from google drive
- Pass the downloaded file into the python-docx module to extract the text
- Pass the text extracted into Django for it to render the Django variables
- Write the text back into docx using the python-docx
- Finally upload the docx file into another google drive account.
I am having problem trying to pass the downloaded file into python-docx as implemented here
Below is my codes in google app engine
downloadUrl = searchResult.get('items')[1]['downloadUrl']
if downloadUrl:
resp, tempContent = drive_service._http.request(downloadUrl)
if resp.status == 200:
f = StringIO.StringIO(tempContent)
document = Document(f)
para = document.paragraphs()
print para
f.close()
The above code gave the following error:
para = document.paragraphs()
TypeError: 'list' object is not callable
This is my codes for rending the extracted text in Django templating that works
myTemplate = Template(tempContent)
c = Context({
"salutation": "William",
"inventionTitle":"Biometric KeyLock"
})
fullContent = myTemplate.render(c)
The mimetype for the downloaded file is:
application/vnd.openxmlformatsofficedocument.wordprocessingml.document
My problem is, i don't know how to process the downloaded file. I want to replace the placeholders/variables in word docx stored in google drive without loosing the formatting then uploaded it back into google drive.
If there is any better way of implementing this, kindly let me know.
Thank you.