Python Forum
python-docx- change lowercase to bold, italic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python-docx- change lowercase to bold, italic
#1
In my docx file there are pieces of text i have to process such as making words italic, bold or underlined.

Example

Quote: input: In the future, if you try hard you will fail a lot


I used this code:

from docx import Document
import re

def change(filename,key):
    doc = Document(filename)
    for p in doc.paragraphs:
        text = p.text
        split_text = text.split(key)
        for i in split_text[:-1]:
            p.add_run(i)
            font = p.add_run(key).bold = True
        p.add_run(split_text[-1])
    doc.save('test.docx')
path = r'path of file'
change(path,'key')
but the result is
Quote:In the future, if you try hard you will fail a lotIn the future

What do I have to do so that the output is

Quote:output: In the future, if you try hard you will fail a lot
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text conversion to lowercase is not working ineuw 3 460 Jan-16-2024, 02:42 AM
Last Post: ineuw
  no module named 'docx' when importing docx MaartenRo 1 848 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 3,302 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx: preserve formatting when printing lines Tmagpy 4 2,089 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,518 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  python-docx regex: replace any word in docx text Tmagpy 4 2,216 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Сombine (Merge) word documents using python-docx Lancellot 1 11,522 May-12-2021, 11:07 AM
Last Post: toothedsword
  How to add run in paragraph using python-docx? toothedsword 0 2,780 May-12-2021, 10:55 AM
Last Post: toothedsword
  How to use python to convert pdf to docx impact_code 3 2,632 Aug-01-2020, 01:58 PM
Last Post: Yoriz
  Extracting parts of paragraphs from word documents using python-docx library & lists Den0st 0 10,603 Nov-06-2019, 12:07 AM
Last Post: Den0st

Forum Jump:

User Panel Messages

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