question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Printing Nested Tables

See original GitHub issue

HI there, I would like to check if it is possible to print nested tables out using this library? I tried to work on it but however, it just prints the nested table as empty.

from docx import Document

f = open('a.docx', 'rb')
doc = Document(f)

table = doc.tables[0]
data = []

keys = None
for i, row in enumerate(table.columns):
    text = (cell.text for cell in row.cells)

    if i == 0:
        keys = tuple(text)
        continue
    row_data = dict(zip(keys, text))
    data.append(row_data)
print (data)

x = 0
for i in doc.paragraphs:
    if x < 20:
        print (i.text)
    else:
        break
    x = x + 1

f.close()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
scannycommented, Jan 15, 2020

I think I would extract the bit about navigating a single table for its paragraphs and make that recursive:

def iter_table_paragraphs(table):
    for row in table.rows:
        for cell in row.cells:
            yield from cell.paragraphs
            for table in cell.tables:
                yield from iter_table_paragraphs(table)       

def print_tables(document):
    for paragraph in iter_table_paragraphs(document):
        print(paragraph.text)

That way your specific use for those paragraphs (printing in this case) is up to the caller and the iter_table_paragraphs() function is more general purpose. You could use it to inspect each paragraph for example, or maybe inspect each paragraph style or search for a word.

1reaction
scannycommented, Jan 8, 2020

@richylyq python-docx supports nested tables just fine, you just need to look for them as tables. They are not part of the text of a cell. Something like:

for cell in cells:
    for table in cell.tables:
        # do something with the nested table.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested table issue while printing document - Stack Overflow
Try the following css (this only helps if you have a very plain thead an only one aswell): thead { display: table-header-group }...
Read more >
PDF/Print with nested tables — DataTables forums
Hello I'm using the feature of export PDF and print button, this works weel since it produce the PDF or launch the print...
Read more >
Printing nested tables in R – bridging between the {reshape ...
This post shows how to print a prettier nested pivot table, created using the {reshape} package (similar to what you would get with ......
Read more >
Oracle PL/SQL - Printing Elements of Sparse Nested Table
The following code uses FIRST, NEXT, and a WHILE LOOP statement to print the elements of an associative array. It prints the elements...
Read more >
Nested Loops C# - Printing Tables - YouTube
# NestedLoops #CsharpLoops Nested Loop C# - Printing Tables Like, Share, And Subscribe | Professor Saad Yousuf … Show more. Show more ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found