如何使用python创建docx文件 [英] how to create docx files with python

查看:304
本文介绍了如何使用python创建docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将数据以Microsoft文字或libreoffice writer的形式放在表中.

I am trying to take my data and put it in tables in either microsoft words or libreoffice writer.

我需要能够更改表格中单元格的背景,并且需要将page属性更改为横向".

I need to be able to change the background of cells within the table and I need to be able to change the page property to 'landscape'.

我一直在寻找一个包含简单代码的库(我是编码的初学者),但是我没有找到需要做的事情.

I have been looking for a library with simple code ( I am a beginner in coding ) but I did not find one for what I need to do.

您有没有为我听到的消息?如果有使用它的示例,那将使我更容易学习它.

Have you heard of anything for me ? If there are example on how to use it that would make it easier for me to learn it.

推荐答案

查看此项目

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

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

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.

这篇关于如何使用python创建docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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