Python docx module-Cover Page of the word document

I am working on an existing word report and trying to do some automation with python docx module. I need to get the report date from database and paste it to "cover page" of the doc but I couldn't find any attribute about cover page in module. How can I do it?

Upvotes: 0

Views: 1392

Answers (1)

Aayush Arora
Aayush Arora

Reputation: 113

What you can do is:

  1. In the word document write any text where you want to replace the date picked from your database eg: dd-mm-yyyy

  2. You can now search for your entered text "dd-mm-yy" in the word file using regular expressions and replace it with the Date you got from your database.

    The code will be as follows:

    def docx_replace_regex(doc_obj,replaceDate):
    
        regex = re.compile(r"dd-mm-yyyy")
    
        for p in doc_obj.paragraphs:
            if regex.search(p.text):            
                p.text = regex.sub(replaceDate, p.text)                   
    
    
        doc.save('generatedDocument.docx')
    
    
    filename = "Your Word Document Path.docx"  
    doc = Document(filename)  
    docx_replace_regex(doc,date) 
    

Upvotes: 0

Related Questions