简体   繁体   English

添加行后表格样式消失

[英]Table style disappears after adding a row

I'm using python-docx to populate a word template that I have The problem is that, I want to apply a particular style to a table I have The table exists already and I'm just extending it like this:我正在使用 python-docx 来填充我拥有的单词模板问题是,我想将特定样式应用于我拥有的表格该表格已经存在,我只是像这样扩展它:

document = Document(path_to_my_docx)
table = document.tables[0]
row = table.add_row()
row.cells[0].text = "amrouna "

But when I add the row, it outputs this但是当我添加行时,它会输出这个在此处输入图像描述 the row containing amrouna is the row I added I read in the python docx documentation, that styles won't apply unless they already exist in the document which is the case here, isn't it?包含 amrouna 的行是我在 python docx 文档中阅读的我添加的行,除非它们已经存在于文档中,否则 styles 将不适用,这里就是这种情况,不是吗? because my table is already styled before I even load/modify it Does anyone know how to either:因为我的表在我什至加载/修改它之前就已经设置了样式有没有人知道如何:

  1. keep the style from changing when adding a row添加行时保持样式不变
  2. Apply the same style to the table once everything is finished一切完成后,对表格应用相同的样式

If you replace the text of the cell object, it will override any styling applied to the table/row/cell/etc.如果您替换单元格object 的文本,它将覆盖应用于表/行/单元格/等的任何样式。 What I found out after much struggle w/ something similar, is that you have to replace the text of the run within the paragraph , within the cell .在与类似的东西进行了多次斗争之后,我发现你必须在单元格内的段落中替换运行的文本。

row.cells[0].paragraphs[0].runs[0].text = "amrouna "

This will preserve the styling from my limited experience... Hopefully it works for you as well.这将保留我有限经验的样式......希望它也适用于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM