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

查看:56
本文介绍了在 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.

推荐答案

虽然 Paragraph.delete() 尚未在 python-docx 中实现,但有一个解决方法此处记录的功能:https://github.com/python-openxml/python-docx/issues/33#issuecomment-77661907

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天全站免登陆