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
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 3 170 Feb-04-2023, 02:12 PM
Last Post: Brandon_Pickert
  Import XML file directly into Excel spreadsheet demdej 0 192 Jan-24-2023, 02:48 PM
Last Post: demdej
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 414 Dec-12-2022, 08:22 PM
Last Post: jh67
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 261 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Appending a row of data in an MS Excel file azizrasul 3 424 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Moving data from one Excel to another and finding maximum profit azizrasul 7 569 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  Can't import csv data JonWayn 4 486 Sep-18-2022, 02:07 AM
Last Post: JonWayn
  get data from excel and find max/min Timmy94 1 473 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 573 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  panda table data kucingkembar 0 572 Mar-01-2022, 10:38 PM
Last Post: kucingkembar

Forum Jump:

User Panel Messages

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