Skip to content

Instantly share code, notes, and snippets.

@typenoob
Created January 6, 2026 06:43
Show Gist options
  • Select an option

  • Save typenoob/ac9d971450ead27f161f7f0226bdd499 to your computer and use it in GitHub Desktop.

Select an option

Save typenoob/ac9d971450ead27f161f7f0226bdd499 to your computer and use it in GitHub Desktop.
python-docx实用函数
def get_abstract_num_styles(doc):
"""
获取抽象编号样式表
"""
# 获取文档的编号定义部分
numbering_part = doc.part.numbering_part
if numbering_part is None:
return None
abstract_num_styles = {}
for abstract_num in numbering_part.element.findall(qn("w:abstractNum")):
abstract_num_id = abstract_num.get(qn('w:abstractNumId'))
levels = {}
for lvl in abstract_num.findall(qn('w:lvl')):
ilvl = lvl.get(qn('w:ilvl'))
num_fmt = lvl.find(qn('w:numFmt')).get(qn('w:val')) if lvl.find(qn('w:numFmt')) is not None else 'decimal'
lvl_text = lvl.find(qn('w:lvlText')).get(qn('w:val')) if lvl.find(qn('w:lvlText')) is not None else '%1.'
start = int(lvl.find(qn('w:start')).get(qn('w:val'))) if lvl.find(qn('w:start')) is not None else 1
levels[ilvl] = {
'num_fmt': num_fmt,
'lvl_text': lvl_text,
'start': start
}
abstract_num_styles[abstract_num_id] = levels
return abstract_num_styles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment