python-docx:转换类型< class' docx.oxml.text.run.ct_r'> to< class' docx.text.run.run.run'>应用亮点

发布于 2025-01-28 15:34:34 字数 396 浏览 6 评论 0 原文

我使用Python-docx具有输出CT_R对象的功能。我希望能够将其转换为一个运行对象,以便我可以应用样式更改,例如:

font.highlight_color = wd_color_index.yellow

,尝试应用造型更改为CT_R对象是错误的。如何转换类型的对象< class'docx.oxml.text.run.ct_r'>到类型的对象< class'docx.text.run.run'>?我试图将

运行(r,段落)

作为ct_r变量编写(r,段落),但是我遇到了一个错误,即“ name error:name'name'run''未定义”。

我认为运行是CT_R的父类,但我无法弄清楚转换。感谢提前的帮助!

I have a function using python-docx which outputs a CT_R object. I'd like to be able to convert it to a Run object such that I can then apply a styling change, something like:

font.highlight_color = WD_COLOR_INDEX.YELLOW

As it stands, trying to apply the styling change to the CT_R object is giving an error. How can I convert the object of type <class 'docx.oxml.text.run.CT_R'> to an object of type <class 'docx.text.run.Run'>? I tried to write

Run(r, paragraph)

with 'r' being the CT_R variable, but I am getting an error that "NameError: name 'Run' is not defined".

I think a run is the parent class of CT_R but I cannot figure out the conversion. Thanks for the help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2025-02-04 15:34:34

相关类应通过

from docx.text.run import Run

The relevant class should be available via https://github.com/python-openxml/python-docx/blob/master/docx/text/run.py

from docx.text.run import Run
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文