Skip to content

how can I replace/add word in docx #443

@robinshion

Description

@robinshion

my simple code is :

import datetime
import docx
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt
from docx.shared import Cm
from docx import section
import os
import sys


lastday=str(datetime.date.today()-datetime.timedelta(days=1))


doc=Document('C:\Users\Administrator\Desktop\python test\update_test\\sample.docx')


for paragraph in doc.paragraphs:
	if 'Report_name' in paragraph.text:
		print paragraph.text
		titlename='Test'+' '+lastday
		paragraph.text = titlename





filename=lastday+' '+'test.docx'
filepath = os.path.join(r'C:\Users\Administrator\Desktop\python test\update_test', filename)

doc.save(filepath)

the old text is Report_name, it is in bold but when I finished changing it change to non-bold how can I keep the style

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions