DOCX: Insert page breaks at the start of every new section

See https://bugs.launchpad.net/calibre/+bug/1196728 for an example
This commit is contained in:
Kovid Goyal 2013-07-03 11:23:26 +05:30
parent 3b4094a890
commit 80f3e7f867
2 changed files with 12 additions and 1 deletions

View File

@ -403,6 +403,11 @@ class Styles(object):
ps.margin_top = 0 ps.margin_top = 0
last_para = p last_para = p
def apply_section_page_breaks(self, paras):
for p in paras:
ps = self.resolve_paragraph(p)
ps.pageBreakBefore = True
def register(self, css, prefix): def register(self, css, prefix):
h = hash(frozenset(css.iteritems())) h = hash(frozenset(css.iteritems()))
ans, _ = self.classes.get(h, (None, None)) ans, _ = self.classes.get(h, (None, None))

View File

@ -100,6 +100,9 @@ class Convert(object):
self.body.append(p) self.body.append(p)
paras.append(wp) paras.append(wp)
self.styles.apply_contextual_spacing(paras) self.styles.apply_contextual_spacing(paras)
# Apply page breaks at the start of every section, except the first
# section (since that will be the start of the file)
self.styles.apply_section_page_breaks(self.section_starts[1:])
notes_header = None notes_header = None
if self.footnotes.has_notes: if self.footnotes.has_notes:
@ -180,6 +183,7 @@ class Convert(object):
def read_page_properties(self, doc): def read_page_properties(self, doc):
current = [] current = []
self.page_map = OrderedDict() self.page_map = OrderedDict()
self.section_starts = []
for p in descendants(doc, 'w:p', 'w:tbl'): for p in descendants(doc, 'w:p', 'w:tbl'):
if p.tag.endswith('}tbl'): if p.tag.endswith('}tbl'):
@ -189,8 +193,10 @@ class Convert(object):
sect = tuple(descendants(p, 'w:sectPr')) sect = tuple(descendants(p, 'w:sectPr'))
if sect: if sect:
pr = PageProperties(sect) pr = PageProperties(sect)
for x in current + [p]: paras = current + [p]
for x in paras:
self.page_map[x] = pr self.page_map[x] = pr
self.section_starts.append(paras[0])
current = [] current = []
else: else:
current.append(p) current.append(p)