Skip to content
Newer
Older
100755 60 lines (44 sloc) 2.71 KB
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
1 #!/usr/bin/env python2.6
2 '''
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored Jan 1, 2010
3 This file makes an docx (Office 2007) file from scratch, showing off most of python-docx's features.
4
5 If you need to make documents from scratch, use this file as a basis for your work.
7c8ae4c @mikemaccana - Cleaned up document properties code
authored Jan 1, 2010
6
7 Part of Python's docx module - http://github.com/mikemaccana/python-docx
8 See LICENSE for licensing information.
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
9 '''
10 from docx import *
11
12 if __name__ == '__main__':
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored Jan 1, 2010
13 # Make a new document tree - this is the main part of a Word document
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
14 document = newdocument()
15
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored Jan 1, 2010
16 # This xpath location is where most interesting content lives
3459ab7 @mikemaccana - Streamlined namespace support by simulating namespace prefixes
authored Jan 2, 2010
17 docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
18
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored Jan 1, 2010
19 # Append two headings and a paragraph
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
20 docbody.append(heading('''Welcome to Python's docx module''',1) )
21 docbody.append(heading('Make and edit docx in 200 lines of pure Python',2))
22 docbody.append(paragraph('The module was created when I was looking for a Python support for MS Word .doc files on PyPI and Stackoverflow. Unfortunately, the only solutions I could find used:'))
23
24 # Add a numbered list
25 for point in ['''COM automation''','''.net or Java''','''Automating OpenOffice or MS Office''']:
26 docbody.append(paragraph(point,style='ListNumber'))
27 docbody.append(paragraph('''For those of us who prefer something simpler, I made docx.'''))
28
29 docbody.append(heading('Making documents',2))
30 docbody.append(paragraph('''The docx module has the following features:'''))
31
32 # Add some bullets
33 for point in ['Paragraphs','Bullets','Numbered lists','Multiple levels of headings','Tables']:
34 docbody.append(paragraph(point,style='ListBullet'))
35
36 docbody.append(paragraph('Tables are just lists of lists, like this:'))
37 # Append a table
38 docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
39
40 docbody.append(heading('Editing documents',2))
41 docbody.append(paragraph('Thanks to the awesomeness of the lxml module, we can:'))
42 for point in ['Search and replace','Extract plain text of document','Add and delete items anywhere within the document']:
43 docbody.append(paragraph(point,style='ListBullet'))
3459ab7 @mikemaccana - Streamlined namespace support by simulating namespace prefixes
authored Jan 2, 2010
44
45 # Add an image (beta)
46 #docbody.append(picture())
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
47
48 # Search and replace
49 document = replace(document,'the','the goshdarned')
50
d00ad4f @mikemaccana - Added support for section and page breaks
authored Dec 31, 2009
51 # Add a pagebreak
52 docbody.append(pagebreak(type='page', orient='portrait'))
53
54 docbody.append(heading('Ideas? Questions? Want to contribute?',2))
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
55 docbody.append(paragraph('''Email <python.docx@librelist.com>'''))
56
c7bdf60 @mikemaccana - Most document properties now editable
authored Dec 31, 2009
57 properties = docproperties('Python docx demo','A practical example of making docx from Python','Mike MacCana',['python','Office Open XML','Word'])
5fe6868 @mikemaccana - Added example demo
authored Dec 17, 2009
58
59 # Save our document
c7bdf60 @mikemaccana - Most document properties now editable
authored Dec 31, 2009
60 savedocx(document,properties,'Welcome to the Python docx module.docx')
Something went wrong with that request. Please try again.