I try to import data from Excel table to Word Template. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I try to import data from Excel table to Word Template. (/thread-30447.html) |
I try to import data from Excel table to Word Template. - NewbiePyPy - Oct-21-2020 Hi, as title says i tried to import data from an excel table to and word table template and after multiple tries i got stuck and don't know what else to do. from mailmerge import MailMerge from openpyxl import load_workbook from docxtpl import DocxTemplate, RichText, InlineImage from docx.shared import Inches, Mm wb = load_workbook('Site1.xlsx') sheet = wb['Sheet1'] max_col = sheet.max_row sheet.delete_rows(sheet.min_row, 1) for i in range (1, max_col): template = 'MOP_DenHaag_V1.0.docx' document1 = MailMerge(template) document1.merge(Migration_ID = str(sheet.cell(row = i , column = 3).value), Group_ID = str(sheet.cell(row = i , column = 2).value), Day = str(sheet.cell(row = i, column = 3).value), PEER = str(sheet.cell(row = i, column = 6).value), Port = str(sheet.cell(row = i, column = 7).value), New_Port = str(sheet.cell(row = i, column = 8).value) ) document1.write('excel2word'+str(sheet.cell(row = i, column = 1).value)+'.docx')This method dosen't work it only create 40+ word files. But i tried like this and is working, but i dont want to write all the lines from the excel manualy. contexttbl = {} resultLinks = [] allLinks = [] linkDic = {'Migration_ID':1,'Group_ID':"Two",'Day':"one"'','PEER':"Router-PE17",'Port':"ethernet 7/0",'New_Port':'port3'} linkDic2 = {'Migration_ID':2,'Group_ID':"Two",'Day':"one"'','PEER':"Router-PE18",'Port':"ethernet 7/1",'New_Port':'port2'} linkDic3 = {'Migration_ID':3,'Group_ID':"Two",'Day':"one"'','PEER':"Router-PE19",'Port':"ethernet 7/2",'New_Port':'port10'} allLinks.append(linkDic) allLinks.append(linkDic2) allLinks.append(linkDic3) contexttbl['links'] = allLinks # doc.render(contexttbl) |