1 fromDocxImportDocument2 fromDocx.sharedImportInches3 4 #New Document Object5Document =Document ()6 7 #add a Paragraph object8Paragraph = document.add_paragraph ("Hello World". Title ())9 Ten #Insert paragraph OneParagraph.insert_paragraph_before ("Python") A - #Add Heading -Document.add_heading ("This is the default heading") the - #Add a page break - #Document.add_page_break () - + #Add a table -Table = document.add_table (rows=2, cols=3) +Table.add_row ()#Adding Rows A at forRowinchTable.rows:#Traversing Tables - forCellinchRow.cells: -Cell.text ="Fuck" -Cell = Table.cell (0,0)#Cell Cell - - #adding images and resizing them inDocument.add_picture ("Test.gif", Width=inches (1.0)) - to #style +Paragraph = document.add_paragraph ("did I looking better?") -Paragraph.style ="Listbullet" the * #Run $Paragraph = document.add_paragraph ("This is before run test")Panax NotoginsengRun = Paragraph.add_run ("This is test run") -Run.bold =True theRun.style ="emphasis" #style +Paragraph.add_run ("run ends hear.") A the #Save Document +Document.save ("Test.docx")
The above is a new docx document
[Library for Python-docx]docx document operations