Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change string in MS word
#1
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
Reply
#2
Did you install that library successfully?
Reply
#3
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
Reply
#4
(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.
Reply
#5
(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.
Reply
#6
(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.
Reply
#7
(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.
Reply
#8
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.
Reply
#9
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 832 Aug-12-2021, 04:25 PM
Last Post: palladium
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 960 May-24-2021, 08:48 AM
Last Post: bowlofred
  How can I change a string. Mike Ru 3 1,254 Jun-03-2020, 10:55 AM
Last Post: buran
  Python Speech recognition, word by word AceScottie 6 12,084 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Change string into Dict Robin_at_Cantelli 2 1,909 Mar-05-2020, 10:37 AM
Last Post: vishalhule
  filter just with the string word jacklee26 2 1,283 Feb-03-2020, 03:25 PM
Last Post: snippsat
  Reverse the string word sneha 2 1,404 Dec-12-2019, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 4,689 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 2,374 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Word replace with string dabozz 1 1,348 Feb-13-2019, 03:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020