Application that process or stores text, images, chart, tables and so on. Python offers Word processing library by which we can manipulate its contents or other objects. Let’s follow the code to read one sample document in python which is referred as “sample.docx“.
python-docx: this library needs to be imported to support the code that will help us to manipulate a word document.
import docx doc = docx.Document('sample.docx') #reading paragraphs by passing integer index print(doc.paragraphs.text) print(doc.paragraphs.text) #using runs method to iterate various attribute #get number of available runs in specific paragraph print(len(doc.paragraphs.runs)) #get Text print(doc.paragraphs.runs.text) for run in doc.paragraphs.runs print(run.text) #Reading complete paragraph completeText =  for para in doc.paragraphs; completeText.append(para.text) print('\n'.join(completeText))
It is very useful property in word which determines look and feel of text within document and can be access as below:
import docx #Reference existing document doc = docx.Document('sample.docx') #print first paragraph print(doc.paragraphs.text) #get first paragraph style doc.paragraphs.style #change style of the paragraph to Normal doc.paragraphs.style = 'Normal' #save document to new document doc.save('myCustomStyle.docx')
Next >> Image Processing in Python