在不丢失当前样式的情况下获取单元格段落



所以,我得到了运行的函数

def get_bold_lines_from_cell(cellColumn, cellRow):  
    for index, paragraph in enumerate(table.cell(cellRow, cellColumn).paragraphs):  
        for run in paragraph.runs:  
            if run.bold:  
                #do stuff  

即使段落中充满了粗体段落,它也无法识别任何段落。它是不是因为我把一个docx变成了表格而失去了风格?有没有办法获得段落样式?

谢谢!

如果有人遇到同样的问题,这就是我提出的解决方案

for table in tables:
    cell = table._cells[cellNumber]
        for paragraphIndex, paragraph in enumerate(cell.paragraphs):
            for parentParagraphsIndex, parentParagraphs in enumerate(paragraph._parent.paragraphs):
                for run in parentParagraphs.runs:
                    tempString =  parentParagraphs.text.encode('utf-8')
                    if run.bold:
                        #do stuff
                        break
                    elif run.style.style_id == "Strong":
                        #do stuff
                        break
                    else:
                        #do stuff
                        break

最好的办法是查看每个对象的XML以找到线索。

print paragraph._element.xml
print run._element.xml

如果应用了样式,您将在w:pPrw:rPr元素中看到它。

最新更新