Permalink
Browse files

- Added support for section and page breaks

- Added support for document narrators to paragraphs
Thanks to Marcin Wielgoszewski for both changes.
  • Loading branch information...
1 parent 1654649 commit d00ad4f3558a3053400d4b4c90e65803b6b51bd4 @mikemaccana committed Dec 31, 2009
Showing with 54 additions and 6 deletions.
  1. +11 −2 README.markdown
  2. +39 −3 docx.py
  3. +4 −1 example.py
View
@@ -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>
View
42 docx.py
@@ -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})
View
@@ -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

0 comments on commit d00ad4f

Please sign in to comment.