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


Messages In This Thread
I try to import data from Excel table to Word Template. - by NewbiePyPy - Oct-21-2020, 12:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a table with different sizes of columns in MS word pepe 8 1,556 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 746 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 802 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,037 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 843 May-29-2023, 02:42 PM
Last Post: Nietzsche
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,854 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Python 3.11 data import question love0715 2 807 Mar-05-2023, 06:50 PM
Last Post: snippsat
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,542 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Import XML file directly into Excel spreadsheet demdej 0 838 Jan-24-2023, 02:48 PM
Last Post: demdej

Forum Jump:

User Panel Messages

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