使用 Python 按字母顺序对 MS Word 段落进行排序
Sort MS Word paragraphs alphabetically with Python
如何使用 python-docx 按字母顺序对 MS Word 段落进行排序?
我尝试了几件事,但无法正常工作。
像下面这样的代码可以完成这项工作吗?
from docx import Document
document = Document()
document.add_paragraph('B - paragraph two' )
document.add_paragraph('A - paragraph one' )
document.paragraphs.sort(key=lambda x: x.text)
document.save('sorted_paragraphs.docx')
sorted_paragraphs.docx
的预期结果:
A - paragraph one
B - paragraph two
即:有没有办法做 MS word GUI sort 对 python 所做的相同事情?
重点是更改文档中段落的位置,以便根据段落首字母按字母顺序显示。
像这样应该可以解决问题:
# --- range of paragraphs you want to sort, by paragraph index
# --- note that the last paragraph (18) is not included, consistent
# --- with Python "slice" notation.
start, end = 8, 18
# --- create a sorted list of tuples (pairs) of paragraph-text (the
# --- basis for the sort) and the paragraph `<w:p>` element for each
# --- paragraph in range.
text_element_triples = sorted(
(paragraph.text, i, paragraph._p)
for i, paragraph in enumerate(document.paragraphs[start:end])
)
# --- move each paragraph element into the sorted position, starting
# --- with the first one in the list
_, _, last_p = text_element_triples[0]
for _, _, p in text_element_triples[1:]:
last_p.addnext(p)
last_p = p
如何使用 python-docx 按字母顺序对 MS Word 段落进行排序?
我尝试了几件事,但无法正常工作。 像下面这样的代码可以完成这项工作吗?
from docx import Document
document = Document()
document.add_paragraph('B - paragraph two' )
document.add_paragraph('A - paragraph one' )
document.paragraphs.sort(key=lambda x: x.text)
document.save('sorted_paragraphs.docx')
sorted_paragraphs.docx
的预期结果:
A - paragraph one
B - paragraph two
即:有没有办法做 MS word GUI sort 对 python 所做的相同事情?
重点是更改文档中段落的位置,以便根据段落首字母按字母顺序显示。
像这样应该可以解决问题:
# --- range of paragraphs you want to sort, by paragraph index
# --- note that the last paragraph (18) is not included, consistent
# --- with Python "slice" notation.
start, end = 8, 18
# --- create a sorted list of tuples (pairs) of paragraph-text (the
# --- basis for the sort) and the paragraph `<w:p>` element for each
# --- paragraph in range.
text_element_triples = sorted(
(paragraph.text, i, paragraph._p)
for i, paragraph in enumerate(document.paragraphs[start:end])
)
# --- move each paragraph element into the sorted position, starting
# --- with the first one in the list
_, _, last_p = text_element_triples[0]
for _, _, p in text_element_triples[1:]:
last_p.addnext(p)
last_p = p