Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################
- # Word docx
- #########################
- import docx
- doc = docx.Document(sFileName) #list of paragraphs
- paragraphs = doc.paragraphs
- paragraphs[0].text
- p = paragraphs[0]
- #in each paragraph there are mltiple Runs (every style change is the start of a new Run)
- p.run[0].text
- p.run[0].bold #it can be True, False, or None. The attribute bold == Note for inherited the style from the paragraph
- p.run[0].italic
- r = p.run[0].underline
- p.run[0].underline = True #change the attribute vale.
- #each run and paragraph has a Style build in
- p.style
- r.style = "Title"
- d.save(sFileName)
- ###write docx starting from empty doc###
- d = docx.Document()
- d.add_paragraph('Hello this is...') #they can be added only at the end. If we want to add one in the middle of a doc we need to create
- #a new document iterating the paragraphs of the current one, and dynamically adding the new paragraph
- d.paragraphs[0].add_run('This is a new run')
- p.runs[0].bold = True
- d.save(sFileName)
- #print all the text in a document forgetting the style. Only put one paragraph after the other separated by the \n
- def getText(sFileName) :
- doc = docx.Document(filename)
- fullText = []
- for p in doc.paragraphs :
- fullText.append(p.text)
- return '\n'.join(fullText)
- print(getText(sFileNmae))
Advertisement
Add Comment
Please, Sign In to add comment