resnews_template_word.py

Home   »   resnews_template_word.py

# create style 


from docx import Document
# create style 
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Inches, Pt

## TO DO 
## [1] add an image in a cell 
## https://stackoverflow.com/questions/32932230
## [2] make image smaller 

def create_style(xdocument, xstyle_name):
    '''
    create style 
    # to do select most important properties 
    '''
    xstyle = xdocument.styles.add_style(xstyle_name, WD_STYLE_TYPE.PARAGRAPH)
    #font 
    xstyle_title.font.name = 'Calibri'
    # size 
    xstyle_title.font.size = Pt(24)    
    #spacing 
    xstyle_title.paragraph_format.space_before = Pt(6)


def add_story_table(xdocument,
                    xtxt_title, xtxt_body,xtxt_link,
                    xstyle_title = 'Heading 1', 
                    xstyle_body  = 'Heading 1',
                    xstyle_link = ''):
    '''
    add story table 
    '''
    #add table 
    xtable = xdocument.add_table(rows=3, cols=1)
    # add text
    xcell_title = xtable.cell(0, 0)
    xcell_title.text = xtxt_title
    xcell_title.paragraphs[0].style = xdocument.styles[xstyle_title]
    # add text body
    xcell_body = xtable.cell(1, 0)
    xcell_body.text = xtxt_body
    xcell_body.paragraphs[0].style = xdocument.styles[xstyle_body]
    # add link
    xcell_link = xtable.cell(1, 0)
    xcell_link.text = xtxt_link
    xcell_link.paragraphs[0].style = xdocument.styles[xstyle_link]
    

# usage 
#from docx import Document
#xdocument = Document()
#xdocument.save('xtest.docx')    
    

Leave a Reply

Your email address will not be published. Required fields are marked *