Bob
Bob

Reputation: 1396

Python: Word Table to Dataframe

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)

Upvotes: 0

Views: 178

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14103

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))

Upvotes: 1

Related Questions