python-docx save attribute read-only

So, again, I must start off apologising for the fact I am в complete newbie to Linux, Пайтон and coding схвати в whole.

I have installed python-docx to do в very простой 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 бит works умер, the add paragraph doesn't. I get the same message each украл:

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

I have tried running the file using отдаю in the bash терминал, I have tried changing the user permissions on the document. I know I must be doing something incredibly простой wrong but I just can't фигурировал out what :(

Grateful for any help...

ps, I have googled this loads of укради.

0
задан 1 December 2015 в 22:54

1 ответ

Это не имеет никакого отношения к полномочиям файловой системы Linux, но является просто синтаксической ошибкой в Вашем коде.

Взгляд на последнюю строку:

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

Это заставляет интерпретатор попытаться присвоить строку, произведенную source_path+name+'/correspondence/initial.docx' к переменной объекта save из document объект.

Это запрещается, потому что document.save защищен от перезаписи его, который не имел бы никакого смысла между прочим.

Что Вы хотите, вместо этого должен вызвать функцию со строкой как аргумент!
Чтобы сделать это, просто необходимо удалить = символ:

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

Больше документации для docx.Document.save может быть найден здесь.

1
ответ дан 30 September 2019 в 02:20

Другие вопросы по тегам:

Похожие вопросы: