Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

ms word - how to create docx files with python

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out this project

And here is a great quick-start guide

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...