# Append a table doc.add(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']])) doc.add(heading('Editing documents',2)) doc.add(paragraph('Thanks to the awesomeness of the lxml module, we can:')) for point in ['Search and replace','Extract plain text of document','Add and delete items anywhere within the document']: doc.add(paragraph(point,style='ListBullet')) pic_paragraph = picture(doc,'python_logo.png','This is a test description') doc.add(pic_paragraph) doc.replace('the awesomeness','the goshdarned awesomeness') # Add a pagebreak doc.add(pagebreak(type='page', orient='portrait')) doc.add(heading('Ideas? Questions? Want to contribute?',2)) doc.add(paragraph('''Email <*****@*****.**>''')) # Setting the meta properties of the document. This part is work in progress. If you create a document from scratch, you # we to set them manually. There are also WebSettings, AppProperties and ContentTypes. At the moment they are initialized # on creating a new DocxDocument. Take a look into meta.py for the various classes available. Plan is to work on those meta classes and expand their functionality to the user later on. doc.core_properties = CoreProperties( title='Creating a docx document in Python from scratch.', creator='markmywords', keywords=['python', 'MS Office', 'docx'] ) # Finally, we save the document. doc.save('Welcome to the Python docx module.docx')
Creating a docx document from scratch and adding some elements to it. """ import os import sys import re # adding the parent directory to PATH path = os.path.abspath(os.path.join(os.path.dirname(__file__),"..")) sys.path.append(path) from docx.document import DocxDocument from docx.elements import * from docx.meta import CoreProperties, WordRelationships #doc = DocxDocument('modify.docx') doc = DocxDocument('modify.docx') # Replacing a string of text with another one. doc.replace('This is a sample document', 'This is a modified document') # replacing placeholder with picture pic_paragraph = picture(doc,'python_logo.png','This is a test description') doc.replace('IMAGE', pic_paragraph) # Adding something to the end of the document. doc.add(heading('Adding another element to the end of this document.',1)) # saving the new document doc.save('modified_document.docx')