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
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 430 Aug-12-2021, 04:25 PM
Last Post: palladium
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 457 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Need help on extract dynamic table data Dr_Strange 0 778 Apr-30-2021, 07:03 AM
Last Post: Dr_Strange
  Load the data from multiple source files to one table amy83 2 771 Apr-27-2021, 12:33 AM
Last Post: Pedroski55
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 1,406 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 2,664 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 633 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 653 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Data extraction from a table based on column and row names tgottsc1 1 943 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 677 Dec-21-2020, 03:24 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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