Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Commit

Permalink
- Added support for section and page breaks
Browse files Browse the repository at this point in the history
- Added support for document narrators to paragraphs
Thanks to Marcin Wielgoszewski for both changes.
  • Loading branch information
mikemaccana committed Dec 31, 2009
1 parent 1654649 commit d00ad4f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
13 changes: 11 additions & 2 deletions README.markdown
Expand Up @@ -47,8 +47,17 @@ Thanks to the awesomeness of the lxml module, we can:
- Egg
- Markdown conversion support

### Authors & Contact

If you have idea, or would like to add functionality contact the Python docx mailing list at <python.docx@librelist.com>

- Mike MacCana - main developer
- Marcin Wielgoszewski - support for breaks & document narrators in paragraphs

### License

Licensed under the [MIT license](http://www.opensource.org/licenses/mit-license.php)
Short version: this code is copyrighted to me (Mike MacCana), I give you permission to do what you want with it except remove my name from the credits. See the MIT license for specific terms.
Short version: this code is copyrighted to me (Mike MacCana), I give you permission to do what you want with it except remove my name from the credits. See the LICENSE file for specific terms.


### Contact
The Python docx mailing list at <python.docx@librelist.com>
42 changes: 39 additions & 3 deletions docx.py
Expand Up @@ -67,14 +67,50 @@ def makeelement(tagname,tagtext=None,tagnamespace=getns(docns,'w'),tagattributes
if tagtext:
newelement.text = tagtext
return newelement


def paragraph(paratext,style='BodyText'):
def pagebreak(type='page', orient='portrait'):
'''Insert a break, default 'page'.
See http://openxmldeveloper.org/forums/thread/4075.aspx
Return our page break element.'''
# Need to enumerate different types of page breaks.
if type not in ['page', 'section']:
raiseError('Page break style "%s" not implemented.' % type)

pagebreak = makeelement('p')
if type == 'page':
run = makeelement('r')
br = makeelement('br',tagattributes={'type':type})

run.append(br)
pagebreak.append(run)

elif type == 'section':
pPr = makeelement('pPr')
sectPr = makeelement('sectPr')
if orient == 'portrait':
pgSz = makeelement('pgSz',tagattributes={'w':'12240','h':'15840'})
elif orient == 'landscape':
pgSz = makeelement('pgSz',tagattributes={'h':'12240','w':'15840', 'orient':'landscape'})

sectPr.append(pgSz)
pPr.append(sectPr)
pagebreak.append(pPr)

return pagebreak

def paragraph(paratext,style='BodyText',breakbefore=False):
'''Make a new paragraph element, containing a run, and some text.
Return the paragraph element.'''
# Make our elements
paragraph = makeelement('p')
run = makeelement('r')
run = makeelement('r')

# Insert lastRenderedPageBreak for assistive technologies like
# document narrators to know when a page break occurred.
if breakbefore:
lastRenderedPageBreak = makeelement('lastRenderedPageBreak')
run.append(lastRenderedPageBreak)

text = makeelement('t',tagtext=paratext)
pPr = makeelement('pPr')
pStyle = makeelement('pStyle',tagattributes={'val':style})
Expand Down
5 changes: 4 additions & 1 deletion example.py
Expand Up @@ -40,7 +40,10 @@
# Search and replace
document = replace(document,'the','the goshdarned')

docbody.append(heading('Ideas? Questions? Want to chat?',2))
# Add a pagebreak
docbody.append(pagebreak(type='page', orient='portrait'))

docbody.append(heading('Ideas? Questions? Want to contribute?',2))
docbody.append(paragraph('''Email <python.docx@librelist.com>'''))

## Fetch all the text out of the document we just created
Expand Down

0 comments on commit d00ad4f

Please sign in to comment.