forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-makedocument.py
executable file
·122 lines (101 loc) · 5.04 KB
/
example-makedocument.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python2.6
'''
This file makes an docx (Office 2007) file from scratch, showing off most of python-docx's features.
If you need to make documents from scratch, use this file as a basis for your work.
Part of Python's docx module - http://github.com/mikemaccana/python-docx
See LICENSE for licensing information.
'''
import shutil
from copy import deepcopy
from tempfile import TemporaryFile, mkdtemp
import docx as dx
if __name__ == '__main__':
# Default set of relationshipships - these are the minimum components of a document
relationships = dx.getRelationships()
# Make a new document tree - this is the main part of a Word document
document = dx.newdocument()
# This xpath location is where most interesting content lives
docbody = document.xpath('/w:document/w:body', namespaces=dx.nsprefixes)[0]
# Append two headings and a paragraph
docbody.append(dx.heading('''Welcome to Python's docx module''',1) )
docbody.append(dx.heading('Make and edit docx in 200 lines of pure Python',2))
docbody.append(dx.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:'))
# Add a numbered list
for point in ['''COM automation''','''.net or Java''','''Automating OpenOffice or MS Office''']:
docbody.append(dx.paragraph(point,style='ListNumber'))
docbody.append(dx.paragraph('''For those of us who prefer something simpler, I made docx.'''))
docbody.append(dx.heading('Making documents',2))
docbody.append(dx.paragraph('''The docx module has the following features:''', font='Arial', fontsize=10))
# Add some bullets
for point in ['Paragraphs','Bullets','Numbered lists','Multiple levels of headings','Tables','Document Properties']:
docbody.append(dx.paragraph(point,style='ListBullet'))
docbody.append(dx.paragraph('Tables are just lists of lists, like this:'))
# Append a table
docbody.append(dx.table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
docbody.append(dx.heading('Editing documents',2))
docbody.append(dx.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']:
docbody.append(dx.paragraph(point, style='ListBullet'))
# Add an image
relationships, picpara = dx.picture(
relationships,
'image1.png',
'This is a test description')
docbody.append(picpara)
docbody.append(dx.paragraph([
('hello', {}),
('2', {'vertAlign': 'superscript'}),
]))
# Append a table with special properties and cells
spec_cell = dx.paragraph([('2', {'vertAlign': 'superscript'})])
t_prop_margin = dx.makeelement('tblCellMar')
for margin_type in ['top', 'left', 'right', 'bottom']:
t_prop_margin.append(dx.makeelement(margin_type, attributes={'w': '0', 'type': 'dxa'}))
CELL_SIZE = 12*30 # twenties of a point
docbody.append(dx.table([['A1',
{'content': spec_cell, 'style': {'vAlign': {'val': 'top'},
'shd': {'fill': '777777'}}},
('A3', 'ttt')],
['B1','B2','B3'],
['C1','C2','C3']],
heading=False,
colw=[CELL_SIZE]*3,
cwunit='dxa', # twenties of a point
borders={'all': {'color': 'AAAAAA'}},
celstyle=[{'align': 'center', 'vAlign': {'val': 'center'}}]*3,
rowstyle={'height': CELL_SIZE},
table_props={'jc': {'val': 'center'},
'__margin__': t_prop_margin,
},
))
# Search and replace
print 'Searching for something in a paragraph ...',
if dx.search(docbody, 'the awesomeness'):
print 'found it!'
else:
print 'nope.'
print 'Searching for something in a heading ...',
if dx.search(docbody, '200 lines'):
print 'found it!'
else:
print 'nope.'
print 'Replacing ...',
docbody = dx.replace(docbody,'the awesomeness','the goshdarned awesomeness')
print 'done.'
# Add a pagebreak
docbody.append(dx.pagebreak(type='page', orient='portrait'))
docbody.append(dx.heading('Ideas? Questions? Want to contribute?',2))
docbody.append(dx.paragraph('''Email <python.docx@librelist.com>'''))
# Create our properties, contenttypes, and other support files
coreprops = dx.coreproperties(
title='Python docx demo',
subject='A practical example of making docx from Python',
creator='Mike MacCana',
keywords=['python','Office Open XML','Word'])
appprops = dx.appproperties()
my_contenttypes = dx.getContentTypes()
my_websettings = dx.websettings()
# Save our document
dx.savedocx(document,coreprops,appprops,my_contenttypes,my_websettings,relationships,'Welcome to the Python docx module.docx')