Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 57 lines (42 sloc) 2.708 kB
5fe6868 @mikemaccana - Added example demo
authored
1 #!/usr/bin/env python2.6
2 '''
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored
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
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
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
13 # Make a new document tree - this is the main part of a Word document
5fe6868 @mikemaccana - Added example demo
authored
14 document = newdocument()
15
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored
16 # This xpath location is where most interesting content lives
4103d24 @mikemaccana - Cleaned up namespace prefix codes
authored
17 docbody = document.xpath('/w:document/w:body', namespaces=docns)[0]
5fe6868 @mikemaccana - Added example demo
authored
18
99ad6a8 @mikemaccana - Added example-extracttext.py - a simple 2 line example of how to ex…
authored
19 # Append two headings and a paragraph
5fe6868 @mikemaccana - Added example demo
authored
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'))
44
45 # Search and replace
46 document = replace(document,'the','the goshdarned')
47
d00ad4f @mikemaccana - Added support for section and page breaks
authored
48 # Add a pagebreak
49 docbody.append(pagebreak(type='page', orient='portrait'))
50
51 docbody.append(heading('Ideas? Questions? Want to contribute?',2))
5fe6868 @mikemaccana - Added example demo
authored
52 docbody.append(paragraph('''Email <python.docx@librelist.com>'''))
53
c7bdf60 @mikemaccana - Most document properties now editable
authored
54 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
55
56 # Save our document
c7bdf60 @mikemaccana - Most document properties now editable
authored
57 savedocx(document,properties,'Welcome to the Python docx module.docx')
Something went wrong with that request. Please try again.