如何使用 python-docx 在 MS Word 中以编程方式实现页面布局中的列 [英] How to programatically implement Columns in page layout as of in MS Word using python-docx

查看:48
本文介绍了如何使用 python-docx 在 MS Word 中以编程方式实现页面布局中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为word文档实现一个设计.我必须使用 python-docx 库以编程方式将该文档的页面布局设置为 2 列布局.请帮忙.

I need to implement a design for word document. I have to programatically set the page layout to 2-column layout for that document using python-docx library. Please help.

推荐答案

我在 google 上搜索了这个问题并关注了您在 stackoverflow 和 google 论坛上的评论,我解决了这个问题,下面的代码对我有帮助,也许任何人都可以使用它:)

I google for this question and follow your comments on stackoverflow and google forum, I solved this problem and the code below helps me, maybe anyone could use it :)

from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn

document = Document()
section = document.sections[0]

sectPr = section._sectPr
cols = sectPr.xpath('./w:cols')[0]
cols.set(qn('w:num'),'2')

document.save('demo.docx')

我测试了这段代码,它帮助我制作了一个两列布局的空 docx 文件.

I tested this code and it helps me make a two cols layout empty docx file.

谢谢高拉夫!

这篇关于如何使用 python-docx 在 MS Word 中以编程方式实现页面布局中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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