From b1463a26d0b2bbcd2035826ccb28e6377b99c337 Mon Sep 17 00:00:00 2001 From: Yuri Chernyavsky Date: Wed, 16 Sep 2015 16:59:37 +0300 Subject: [PATCH 1/3] Restart numbering --- docx/document.py | 14 ++++++++++ docx/oxml/numbering.py | 4 ++- docx/parts/numbering.py | 3 +++ docx/text/paragraph.py | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/docx/document.py b/docx/document.py index 655a70e95..4a0ec0692 100644 --- a/docx/document.py +++ b/docx/document.py @@ -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 @@ -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): """ diff --git a/docx/oxml/numbering.py b/docx/oxml/numbering.py index aeedfa9a0..097f3d6ef 100644 --- a/docx/oxml/numbering.py +++ b/docx/oxml/numbering.py @@ -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 () 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): diff --git a/docx/parts/numbering.py b/docx/parts/numbering.py index e324c5aac..186c6f80a 100644 --- a/docx/parts/numbering.py +++ b/docx/parts/numbering.py @@ -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) diff --git a/docx/text/paragraph.py b/docx/text/paragraph.py index 4fb583b94..2401dccb1 100644 --- a/docx/text/paragraph.py +++ b/docx/text/paragraph.py @@ -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 From 10345453bd8e48fbd321421c7dd75f90f134cee4 Mon Sep 17 00:00:00 2001 From: Yuri Chernyavsky Date: Tue, 22 Nov 2016 10:58:57 +0200 Subject: [PATCH 2/3] Test --- HISTORY.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index a51e27043..430333d71 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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() From 80ca2ea2e33ee3c3592513e5cc4b984d2b6ff0e7 Mon Sep 17 00:00:00 2001 From: Yuri Chernyavsky Date: Wed, 19 Jul 2017 17:00:45 +0300 Subject: [PATCH 3/3] Add shared IntOrPercent object --- docx/shared.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docx/shared.py b/docx/shared.py index 919964325..cb93f549a 100644 --- a/docx/shared.py +++ b/docx/shared.py @@ -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): """