Python Forum
I try to import data from Excel table to Word Template.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I try to import data from Excel table to Word Template.
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data extraction from a table based on column and row names tgottsc1 1 109 Jan-09-2021, 10:04 PM
Last Post: buran
  xml file creation from an XML file template and data from an excel file naji_python 1 126 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Load data from One oracle Table to Multiple tables amy83 1 155 Dec-02-2020, 01:57 AM
Last Post: Larz60+
  Mail Merge from DataFrame into Word Label Template PEGylated_User 0 138 Nov-10-2020, 01:01 PM
Last Post: PEGylated_User
  Extract data from PDF page to Excel nathan_nz 1 202 Oct-29-2020, 08:04 PM
Last Post: Larz60+
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 497 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Adding data to a table in SQLite3 djwilson0495 2 458 Aug-15-2020, 02:48 PM
Last Post: djwilson0495
  Import CSV data into array and turn into integers DoctorSmiles 5 531 Jul-16-2020, 10:47 AM
Last Post: perfringo
  data frame excel cell calulation buunaanaa 1 544 Jul-04-2020, 06:00 PM
Last Post: buunaanaa
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 395 Jun-26-2020, 11:59 AM
Last Post: Mangesh121

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020