Python Forum
change string in MS word - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: change string in MS word (/thread-34949.html)



change string in MS word - Mr_Blue - Sep-18-2021

hello,
i would like to change a word in a word document.

from docx import Document
document = Document('/path/word_template.docx')
doc = docx.Document()

VAR1 = input('FIRST_NAME: ')
VAR2 = input('LAST_NAME: ')

for paragraph in document.paragraphs:
    if 'FIRST_NAME' in paragraph.text:
        print paragraph.text
        paragraph.text = VAR1
    elif 'LAST_NAME' in paragraph.text
        print paragraph.text
        paragraph.text = VAR1

doc.save('/path/word_template.docx')
Error:
Traceback (most recent call last): File "test1.py", line 1, in <module> from docx import Document ModuleNotFoundError: No module named 'docx'
how odd.

OS: linux
python 2 et 3 used to no avail



RE: change string in MS word - ndc85430 - Sep-18-2021

Did you install that library successfully?


RE: change string in MS word - bowlofred - Sep-18-2021

It's not that it's not installed, it's that it's imported as Document, so the name docx can't be used here.

Line 3 isn't necessary
doc should be document on line 16

Line 11 replaces the entire paragraph with the first name, not just the FIRST_NAME string.
Line 14 does the same, but it uses the first name variable again


RE: change string in MS word - snippsat - Sep-18-2021

(Sep-18-2021, 09:13 PM)bowlofred Wrote: It's not that it's not installed, it's that it's imported as Document, so the name docx can't be used here.
It's not installed as it fail on docx before import of Document.
So the library is python-docx install is pip install python-docx
If i copy his first line as i do have it installed.
>>> from docx import Document
>>> 
>>> help(Document)
Help on function Document in module docx.api:

Document(docx=None)
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object. If
    *docx* is missing or ``None``, the built-in default document "template"
    is loaded.



RE: change string in MS word - bowlofred - Sep-19-2021

(Sep-18-2021, 11:15 PM)snippsat Wrote: It's not installed as it fail on docx before import of Document.

Indeed. I misread the traceback as failing on the second import, not the first.


RE: change string in MS word - Mr_Blue - Sep-19-2021

(Sep-18-2021, 11:15 PM)snippsat Wrote:
(Sep-18-2021, 09:13 PM)bowlofred Wrote: It's not that it's not installed, it's that it's imported as Document, so the name docx can't be used here.
It's not installed as it fail on docx before import of Document.
So the library is python-docx install is pip install python-docx
If i copy his first line as i do have it installed.

i realize there's much more to changing a string than first thought.
for now, i wish to install python docx on my machine (OS: ubuntu 18.04)
will pip install python-docx work in the termiinal, you think ? i'm weary of making a mistake.
anyway, i kindly ask you to feed this thread because i think that python-docx doesn't provide any simple way of changing a string.
it might come in handy for some folks out there.
thank you again for you help.


RE: change string in MS word - snippsat - Sep-19-2021

(Sep-19-2021, 08:53 AM)Mr_Blue Wrote: i wish to install python docx on my machine (OS: ubuntu 18.04)
will pip install python-docx work in the termiinal, you think ? i'm weary of making a mistake.
anyway, i kindly ask you to feed this thread because i think that python-docx doesn't provide any simple way of changing a string.
it might come in handy for some folks out there.
Ubuntu 18.04 a little old now came with Python 3.6 and Python 2.7.
Test from command line(Terminal) python3 -V and pip3 -V.
If pip3 is not installed do:
sudo apt-get update
sudo apt install python3-pip 
Install will be pip3 install python-docx.
After install you type python3 in Terminal and test that import works.
Mr_Blue Wrote:i think that python-docx doesn't provide any simple way of changing a string
It has nothing to with changing a normal string as Python do that fine alone.
It's only for working with and manipulating Microsoft Word(.docx) files.


RE: change string in MS word - Mr_Blue - Sep-19-2021

Quote:It has nothing to with changing a normal string as Python do that fine alone.
It's only for working with and manipulating Microsoft Word(.docx) files.


i didn't get something, then.
because when i try to use just python with a command like file.write(line.replace('this', 'that')) i have an error message

Error:
traceback (most recent call last): File "test.py", line 6, in <module> for line in input: File "/usr/lib/python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 253: invalid continuation byte
i gather this is because linux doesn't like windows codecs. i could have changed this where i dealing with "just" a text file, saving it with the UTF-8 thingy. this won't do with a docx file.


RE: change string in MS word - snippsat - Sep-19-2021

(Sep-19-2021, 01:30 PM)Mr_Blue Wrote: i didn't get something, then.
because when i try to use just python with a command like file.write(line.replace('this', 'that')) i have an error message
Open file as binary(rb) then there is no encoding problems.
You most look at doc or search how replace is done python-docx.
Quick test.
from docx import Document

file = open('Hello world.docx', 'rb')
document = Document(file)
for paragraph in document.paragraphs:
    if 'Hello' in paragraph.text:
        print(paragraph.text) # Hello world
        paragraph.text = paragraph.text.replace("Hello", "Ocean")

document.save('result.docx')
So now in result.docx it will say Ocean world