在Python-Docx中从单元格中删除段落 [英] Removing Paragraph From Cell In Python-Docx

查看:1023
本文介绍了在Python-Docx中从单元格中删除段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有两行标题的表,该表的所有样式均使用简单的模板格式.两行标题是必需的,因为在两个主要类别下我具有相同的标题.看来,在Word中处理此问题的唯一方法是使文档在整个页面中重复头格式和进行格式化,这是将两行表嵌套到主内容表的头行中.

I am attempting to create a table with a two row header that uses a simple template format for all of the styling. The two row header is required because I have headers that are the same under two primary categories. It appears that the only way to handle this within Word so that a document will format and flow with repeating header across pages is to nest a two row table into the header row of a main content table.

在Python-DocX中,始终使用单个空的段落元素创建表单元格.对于我的用例,我需要能够完全删除此空的段落元素,而不仅仅是用空字符串清除它.否则我的嵌套表上方会出现换行符,这会破坏我对单个表的幻想.

In Python-DocX a table cell is always created with a single empty paragraph element. For my use case I need to be able to remove this empty paragraph element entirely not simply clear it with an empty string. Or else I have line break above my nested table that ruins my illusion of a single table.

所以问题是如何删除空白段落?

So the question is how do you remove the empty paragraph?

如果您知道一种更好的方法来处理两行标头的实现...那也将是值得的信息.

If you know of a better way to handle the two row header implementation... that would also be appreciated info.

推荐答案

python-docx中尚未实现Paragraph.delete()的情况下,此处记录了一种变通方法:

While Paragraph.delete() is not implemented yet in python-docx, there is a workaround function documented here: https://github.com/python-openxml/python-docx/issues/33#issuecomment-77661907

请注意,表格单元格必须始终以段落结尾.因此,您需要在表后添加一个空的,否则我认为在尝试加载文档时会出现所谓的修复步骤"错误.

Note that a table cell must always end with a paragraph. So you'll need to add an empty one after your table otherwise I believe you'll get a so-called "repair-step" error when you try to load the document.

如果没有多余的段落来确认,可能值得一试;我希望没有它看起来会更好,但是上次我尝试得到错误.

Probably worth a try without the extra paragraph just to confirm; I'm expect it would look better without it, but last time I tried that I got the error.

这篇关于在Python-Docx中从单元格中删除段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆