简体   繁体   中英

python-docx add table object to document

I want to add docx.table.Table and docx.text.paragraph.Paragraph objects to documents. Currently

table = document.add_table(rows=2, cols=2)

Would create a new table inside the document, and table would hold the docx.table.Table object with all its properties.

What I want to do instead is add a table OBJECT to the document that I previously read from another document for example.

I'm guessing the iterating through every property of the newly added table and the table object I read earlier and setting the values would be enough, but is there an alternative method?

Thank you!

There are a few different possibilities your description would admit, but none of them have direct API support in python-docx .

The simplest case is copying a table from one part of a python-docx Document object to another location in the same document . This can probably be accomplished by doing a deep copy of the XML for the table. The details of how to do this are beyond the scope of this question, but there are some examples out there if you search on "python-docx" OR "python-pptx" deepcopy .

More complex is copying a table between one Document object and another. A table may contain external references that are available in the source document but not the target document. Consequently, a deepcopy approach will not always work in this case without locating and resolving any dependencies.

Finally, there is copying/embedding a table OLE object, such as might be found in a PowerPoint presentation or formed from a range in an Excel document. Embedding OLE objects is not supported and is not likely to be added anytime soon, mostly because of the obscurity of the OLE format embedding format (not well documented).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM