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
  Mail Merge from DataFrame into Word Label Template PEGylated_User 0 81 Nov-10-2020, 01:01 PM
Last Post: PEGylated_User
  Extract data from PDF page to Excel nathan_nz 1 133 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 423 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Adding data to a table in SQLite3 djwilson0495 2 384 Aug-15-2020, 02:48 PM
Last Post: djwilson0495
  Import CSV data into array and turn into integers DoctorSmiles 5 436 Jul-16-2020, 10:47 AM
Last Post: perfringo
  data frame excel cell calulation buunaanaa 1 493 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 328 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Support required to data log in SQL server single table via raspbeery pi rithikvg 8 641 Jun-23-2020, 05:23 PM
Last Post: buran
  Exporting data from python into excel Zankawah 5 517 Jun-02-2020, 03:17 AM
Last Post: buran
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 368 May-27-2020, 11:47 PM
Last Post: mcmxl22

Forum Jump:

User Panel Messages

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