Search code examples
pythonpandasopenpyxlxlrdpython-docx

Python: print each excel row as its own ms word page


I've got an excel document with thousands of rows, each row represents a person's record. How can I use Python to extract each row and write that information into an MS Word page?

My goal is to create a doc containing a pseudo-narrative of each record on its own page.


Solution

  • You can extract the content of the Excel file as a Pandas Data Frame and then export the Data Frame into Word Document as a table. This is the generic syntax to achieve your objective

    import pandas as pd
    xls_file = pd.ExcelFile('../data/example.xls')
    df = xls_file.parse('Sheet1')
    
    #Needs PyWin32
    wordApp = win32.gencache.EnsureDispatch('Word.Application')
    wordApp.Visible = False
    doc = wordApp.Documents.Open(os.getcwd()+'\\template.docx')
    rng = doc.Bookmarks("PUTTABLEHERE").Range
    
    # creating Table 
    # add one more row in table at word because you want to add column names as header
    Table=rng.Tables.Add(rng,NumRows=df.shape[0]+1,NumColumns=df.shape[1])
    
    for col in range(df.shape[1]):        
    # Writing column names 
        Table.Cell(1,col+1).Range.Text=str(df.columns[col]) 
        for row in range(df.shape[0]):
        # writing each value of data frame 
            Table.Cell(row+1+1,col+1).Range.Text=str(df.iloc[row,col])
    

    Hope this helps!