小编joh*_*mes的帖子

python-docx - 显示为正常段落的列表

我试图将数字和项目符号列表插入现有的Word文档,但它们显示为正常段落:

# Open up existing document
document = Document('existing_document.docx')

# Add style from example document
temp_doc = Document()
document.styles.add_style('List Number', temp_doc.styles['List Number'].type)

# Add bullet points from example document
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 1')
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 2')

# Save
document.save('new_doc_with_list.docx')
Run Code Online (Sandbox Code Playgroud)

这导致3段.没有缩进或编号.

我也尝试设置所有属性相同,然后完成运行,但这也不起作用:

document.styles['List Number'].base_style = temp_doc.styles['List Number'].base_style
document.styles['List Number'].next_paragraph_style = temp_doc.styles['List Number'].next_paragraph_style
document.styles['List Number'].paragraph_format.alignment = temp_doc.styles['List Number'].paragraph_format.alignment
document.styles['List Number'].paragraph_format.first_line_indent = temp_doc.styles['List Number'].paragraph_format.first_line_indent
document.styles['List Number'].paragraph_format.keep_together = temp_doc.styles['List Number'].paragraph_format.keep_together
document.styles['List Number'].paragraph_format.keep_with_next = …
Run Code Online (Sandbox Code Playgroud)

python python-docx

2
推荐指数
1
解决办法
2640
查看次数

标签 统计

python ×1

python-docx ×1