Skip to content

Commit c31d882

Browse files
author
Jason Ward
committed
Merge branch 'master' into issue_23
2 parents f486348 + dd7be17 commit c31d882

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pydocx/DocxParser.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _set_last_list_item(self, num_ids, list_elements):
275275
last_el = filtered_list_elements[-1]
276276
last_el.is_last_list_item_in_root = True
277277

278-
def _set_headers(self, list_elements):
278+
def _set_headers(self, elements):
279279
# These are the styles for headers and what the html tag should be if
280280
# we have one.
281281
headers = {
@@ -290,17 +290,21 @@ def _set_headers(self, list_elements):
290290
'heading 9': 'h6',
291291
'heading 10': 'h6',
292292
}
293-
for list_item in list_elements:
294-
style = list_item.find_first('pStyle').attrib['val']
293+
for element in elements:
294+
# This element is using the default style which is not a heading.
295+
if element.find_first('pStyle') is None:
296+
continue
297+
style = element.find_first('pStyle').attrib['val']
295298
style = self.styles_dict.get(style)
296-
# Check to see if this list item is actually a header.
299+
300+
# Check to see if this element is actually a header.
297301
if style and style.lower() in headers:
298-
# Set all the list item variables back to false.
299-
list_item.is_list_item = False
300-
list_item.is_first_list_item = False
301-
list_item.is_last_list_item = False
302+
# Set all the list item variables to false.
303+
element.is_list_item = False
304+
element.is_first_list_item = False
305+
element.is_last_list_item = False
302306
# Prime the heading_level
303-
list_item.heading_level = headers[style.lower()]
307+
element.heading_level = headers[style.lower()]
304308

305309
def _set_next(self, body):
306310
def _get_children(el):
@@ -355,7 +359,10 @@ def parse_begin(self, el):
355359

356360
self._set_first_list_item(num_ids, ilvls, list_elements)
357361
self._set_last_list_item(num_ids, list_elements)
358-
self._set_headers(list_elements)
362+
p_elements = [
363+
child for child in body.find_all('p')
364+
]
365+
self._set_headers(p_elements)
359366
self._set_next(body)
360367

361368
self._parsed += self.parse(el)

0 commit comments

Comments
 (0)