且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用python创建docx文件

更新时间:2023-02-19 20:30:42

查看此项目

这是一个很好的快速入门指南

使用非常简单,我还没有测试过,但是它应该可以工作:

It's pretty simple to use, i haven't tested this, but it should work:

from docx import Document

document = Document()
r = 2 # Number of rows you want
c = 2 # Number of collumns you want
table = document.add_table(rows=r, cols=c)
table.style = 'LightShading-Accent1' # set your style, look at the help documentation for more help
for y in range(r):
    for x in range(c):
        cell.text = 'text goes here'
document.save('demo.docx') # Save document

它不认为您可以使用该库设置页面方向属性,但是您可以做的是创建一个空白的Word文档,该文档自己处于横向状态,将其存储在工作目录中,并每次创建一个副本您会生成此文档.

It don't think you can set the page orientation property with this library, but what you could do is create a blank word document that is in landscape yourself, store it in the working directory and make a copy of it every time you generate this document.