Skip to content

Add ability to restart numbering #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Release History
---------------

0.8.5 (2015-02-21)
0.8.6 (2015-02-21)
++++++++++++++++++

- Fix #149: KeyError on Document.add_table()
Expand Down
14 changes: 14 additions & 0 deletions docx/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def add_page_break(self):
paragraph.add_run().add_break(WD_BREAK.PAGE)
return paragraph

def get_new_list(self, abstractNumId):
"""
Returns a new numId that references given abstractNumId
"""
return self.numbering.numbering_definitions.add_num(abstractNumId, True)

def add_paragraph(self, text='', style=None):
"""
Return a paragraph newly added to the end of the document, populated
Expand Down Expand Up @@ -156,6 +162,14 @@ def styles(self):
"""
return self._part.styles

@property
def numbering(self):
"""
A "Provides access to numbering part
"""
x=self._part.numbering_part
return self._part.numbering_part

@property
def tables(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion docx/oxml/numbering.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ class CT_Numbering(BaseOxmlElement):
"""
num = ZeroOrMore('w:num', successors=('w:numIdMacAtCleanup',))

def add_num(self, abstractNum_id):
def add_num(self, abstractNum_id, restart=False):
"""
Return a newly added CT_Num (<w:num>) element referencing the
abstract numbering definition identified by *abstractNum_id*.
"""
next_num_id = self._next_numId
num = CT_Num.new(next_num_id, abstractNum_id)
if restart:
num.add_lvlOverride(ilvl=0).add_startOverride(1)
return self._insert_num(num)

def num_having_numId(self, numId):
Expand Down
3 changes: 3 additions & 0 deletions docx/parts/numbering.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ def __init__(self, numbering_elm):
super(_NumberingDefinitions, self).__init__()
self._numbering = numbering_elm

def add_num(self, abstractNum_id, restart=False):
return self._numbering.add_num(abstractNum_id, restart).numId

def __len__(self):
return len(self._numbering.num_lst)
11 changes: 11 additions & 0 deletions docx/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def __new__(cls, twips):
emu = int(twips * Length._EMUS_PER_TWIP)
return Length.__new__(cls, emu)

class IntOrPercent(object):
"""
Integer or a percentage value
"""
def __init__(self, str):
if str[-1] == "%":
self.is_percent = True
self.value = int(str[:-1])
else:
self.is_percent = False
self.value = int(str)

class RGBColor(tuple):
"""
Expand Down
58 changes: 58 additions & 0 deletions docx/text/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,64 @@ def text(self, text):
self.clear()
self.add_run(text)

@property
def num_id(self):
"""
Return the numId of the parent list.
"""
if self._p.pPr is None:
return None
if self._p.pPr.numPr is None:
return None
if self._p.pPr.numPr.numId is None:
return None
return self._p.pPr.numPr.numId.val

@num_id.setter
def num_id(self, num_id):
"""
Set the numId of the parent list.
"""
self._p.get_or_add_pPr().get_or_add_numPr().get_or_add_numId().val = num_id

@property
def level(self):
"""
Return the indentation level of the parent list.
"""
if self._p.pPr is None:
return None
if self._p.pPr.numPr is None:
return None
if self._p.pPr.numPr.ilvl is None:
return None
return self._p.pPr.numPr.ilvl.val

@level.setter
def level(self, lvl):
"""
Set the indentation level of the parent list.
"""
self._p.get_or_add_pPr().get_or_add_numPr().get_or_add_ilvl().val = lvl

@property
def ind_left(self):
"""
Return the indentation level of the parent list.
"""
if self._p.pPr is None:
return None
if self._p.pPr.ind is None:
return None
return self._p.pPr.ind_left

@level.setter
def ind_left(self, left):
"""
Set the indentation level of the parent list.
"""
self._p.get_or_add_pPr().ind_left=left

def _insert_paragraph_before(self):
"""
Return a newly created paragraph, inserted directly before this
Expand Down