0

I'm trying to put a Word Table into a DataFrame using the code below...

def writejsontable(theDataFrame):
    print(theDataFrame)
    print('-----------')

for block in iter_block_items(doc):
    tablestringarray = []
    if isinstance(block, Table):
        df = [['' for i in range(len(block.columns))] for j in range(len(block.rows))]
        for i, row in enumerate(block.rows):
            for j, cell in enumerate(row.cells):
                df[i][j] = block.cell(i,j).text
        writejsontable(df)

The code runs, but when I go to print the output it is...

[['PERFORMANCE MEASURES', 'GO', 'NO-GO', 'N/A'], ['1. Put on Body Substance Isolation.', '', '', ''], ['2. Opened the airway used a manual maneuver.', '', '', '']]

Is this how dataframes are normally printed? I've seen other examples where dataframes are printed in nice table like structures when you call a print on them. I'm not sure why i'm not getting that nice and neat table like structure when I'm calling a print. Any and all help is much appreciated!

EDIT:

def iter_block_items(parent):
    # Get parrent element
    if isinstance(parent, Document):
        parent_elm = parent.element.body
    elif isinstance(parent, _Cell):
        parent_elm = parent._tc
    else:
        raise ValueError("something's not right")
    # Get children in parent element
    for child in parent_elm.iterchildren():
        if isinstance(child, CT_P):
            yield Paragraph(child, parent)
        elif isinstance(child, CT_Tbl):
            yield Table(child, parent)
Bob
  • 1,344
  • 3
  • 29
  • 63
  • https://stackoverflow.com/questions/19124601/pretty-print-an-entire-pandas-series-dataframe Does this answer help at all? – Josh Zwiebel Apr 29 '20 at 19:27
  • df is a list of lists in your example not a dataframe: `df = [['' for i in range(len(block.columns))] for j in range(len(block.rows))]` – It_is_Chris Apr 29 '20 at 19:27
  • @Yo_Chris that makes sense. I'm trying to create an empty Dataframe with correct rows / columns numbers and then populate it. Is there a better way to do this? – Bob Apr 29 '20 at 19:38
  • @Bob I can take a look. Can you please provide and example for `block` – It_is_Chris Apr 29 '20 at 19:39
  • @Yo_Chris Basically it's just a `Table`. I'll throw the code in my post – Bob Apr 29 '20 at 19:40
  • 1
    @Bob does just creating a dataframe from the array work? `writejsontable(pd.DataFrame(df))` – It_is_Chris Apr 29 '20 at 19:47
  • 1
    @Yo_Chris that was embarrassingly simple! Throw that in an answer and i'd happily accept. Works like a charm! – Bob Apr 29 '20 at 19:54

1 Answers1

1

Your variable df is currently a list of lists and not a pandas.DataFrame You can convert the array df into a frame by using pandas.DataFrame(df)

# df = [['' for i in range(len(block.columns))] for j in range(len(block.rows))]
df= [['PERFORMANCE MEASURES', 'GO', 'NO-GO', 'N/A'],
     ['1. Put on Body Substance Isolation.', '', '', ''],
     ['2. Opened the airway used a manual maneuver.', '', '', '']]

writejsontable(pd.DataFrame(df))
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41