0

So, again, I must start off apologising for the fact I am a complete newbie to Linux, Python and coding as a whole.

I have installed python-docx to do a very simple task:

import shutil
from docx import Document

name = 'xxxxxxxxxx'
source_path = r'/home/tye/Documents/xxxxxxxxxx/'
template = r'template_xx'

shutil.copytree(source_path+template, source_path+name)
document = Document(source_path+name+'/correspondence/initial.docx')
paragraph = document.add_paragraph ('helloworld')
document.save=(source_path+name+'/correspondence/initial.docx')

whilst the folder copying bit works fine, the add paragraph doesn't. I get the same message each time:

'Document' object attribute 'save' is read-only'

I have tried running the file using sudo in the bash terminal, I have tried changing the user permissions on the document. I know I must be doing something incredibly simple wrong but I just can't figure out what :(

Grateful for any help...

ps, I have googled this loads of times.

1

This has nothing to do with Linux file system permissions, but is just a syntactical mistake in your code.

Look at the last line:

document.save=(source_path+name+'/correspondence/initial.docx')

This makes the interpreter try to assign the string produced by source_path+name+'/correspondence/initial.docx' to the object variable save of the document object.

That is forbidden, because document.save is protected against overwriting it, which would make no sense by the way.

What you want instead is to call the function with the string as argument!
To do this, you just need to remove the = symbol:

document.save(source_path+name+'/correspondence/initial.docx')

More documentation for docx.Document.save can be found here.

| improve this answer | |
  • Thanks so much. It's pretty obvious when someone points it out but I would never have thought of that. Just have to figure out how to mark the questions solved... – grumpasaur Dec 1 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.