how to create docx files with python

后端 未结 1 1791
广开言路
广开言路 2020-12-22 08:12

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 tabl

相关标签:
1条回答
  • 2020-12-22 08:49

    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.

    0 讨论(0)
提交回复
热议问题