Icon

kn_​forum_​56177_​excel_​table_​to_​word_​docx

batch print Excel Tables to JPG and save them in a Word Document (.docx)

batch print Excel Tables to JPG and save them in a Word Document (.docx)
created with the help of: https://chat.openai.com/chat

Python code generated with the help of "ChatGPT Dec 15 Version"https://help.openai.com/en/articles/6825453-chatgpt-release-notes write the Excel tables into JPG files and also into DOCX files just create the dummy data excel files batch print Excel Tables to JPG and save them in a Word Document (.docx)created with the help of: https://chat.openai.com/chat import knime.scripting.io as knio# created with the help of: https://chat.openai.com/chatimport docxfrom openpyxl import load_workbookimport osvar_excel_file = knio.flow_variables['File path']var_docx_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + "_table.docx"# Open the Excel filewb = load_workbook(var_excel_file)# Select the sheet with the tablews = wb['default_1']# Create a new Word documentdocument = docx.Document()# Add a table to the document with the same number of rows and columns as the Excel tabletable = document.add_table(ws.max_row, ws.max_column)# Set the widths of the columns in the Word table to match the widths of the columns in the Excel table# for i, column in enumerate(ws.columns):# table.columns[i].width = column.width# Iterate through the rows and cells of the Excel table and add the data and styles to the Word tablefor i, row in enumerate(ws.rows): for j, cell in enumerate(row): # Get the style of the cell style = cell.style # Set the style of the cell in the Word table table.cell(i, j).paragraphs[0].style = style # Set the value of the cell in the Word table table.cell(i, j).text = str(cell.value)# Save the Word documentdocument.save(var_docx_file) import knime.scripting.io as knio# created with the help of: https://chat.openai.com/chatimport docximport openpyxlfrom openpyxl import load_workbookimport osfrom PIL import Image, ImageDraw, ImageFontvar_excel_file = knio.flow_variables['File path']var_docx_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + "_image.docx"var_jpg_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + ".jpg"# Create a new Word documentdocument = docx.Document()# Open the Excel filewb = load_workbook(var_excel_file)# Select the sheet with the tablews = wb['default_1']# you might have to edit thisrange = ws['A1:E151']# Create an image from the rangeimg = Image.new('RGB', (len(range[0])*100, len(range)*20))draw = ImageDraw.Draw(img)# Set the font to use for the cell valuesfont = ImageFont.truetype('Arial', 14)# Iterate through the cells in the range and draw them onto the imagefor row in range: for cell in row: x = cell.column - 1 y = cell.row - 1 # Set the cell's background color to white if the fgColor attribute is not set or is invalid if cell.fill.fgColor is None or cell.fill.fgColor.rgb == '00000000': color = (255, 255, 255) else: color = cell.fill.fgColor.rgb draw.rectangle((x*100, y*20, x*100+99, y*20+19), fill=color) # Add the cell's value with the correct font and size draw.text((x*100+5, y*20+5), str(cell.value), font=font, fill=(0,0,0))# Save the image to a JPG fileimg.save(var_jpg_file , 'JPEG')# Insert the image into the Word documentdocument.add_picture(var_jpg_file)# Save the Word documentdocument.save(var_docx_file) createDummy DatacreateExcel Filescreate4 dummyexcel filesENDCreate Excel Filescreate theExcel File NameslistExcel Filesfrom/data/ folderthat start with the Name"Cluster*"extractFile NamesSTARTto writeto single excelFileENDXLSX -> DOCXas Textpy39_knime_docxXLSX -> DOCXas JPG Data Generator Group Loop Start Excel Writer Variable Loop End Create File/FolderVariables List Files/Folders Path to URI URL to File Path Table Row ToVariable Loop Start Variable Loop End Python Script Conda EnvironmentPropagation Python Script Merge Variables Python code generated with the help of "ChatGPT Dec 15 Version"https://help.openai.com/en/articles/6825453-chatgpt-release-notes write the Excel tables into JPG files and also into DOCX files just create the dummy data excel files batch print Excel Tables to JPG and save them in a Word Document (.docx)created with the help of: https://chat.openai.com/chat import knime.scripting.io as knio# created with the help of: https://chat.openai.com/chatimport docxfrom openpyxl import load_workbookimport osvar_excel_file = knio.flow_variables['File path']var_docx_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + "_table.docx"# Open the Excel filewb = load_workbook(var_excel_file)# Select the sheet with the tablews = wb['default_1']# Create a new Word documentdocument = docx.Document()# Add a table to the document with the same number of rows and columns as the Excel tabletable = document.add_table(ws.max_row, ws.max_column)# Set the widths of the columns in the Word table to match the widths of the columns in the Excel table# for i, column in enumerate(ws.columns):# table.columns[i].width = column.width# Iterate through the rows and cells of the Excel table and add the data and styles to the Word tablefor i, row in enumerate(ws.rows): for j, cell in enumerate(row): # Get the style of the cell style = cell.style # Set the style of the cell in the Word table table.cell(i, j).paragraphs[0].style = style # Set the value of the cell in the Word table table.cell(i, j).text = str(cell.value)# Save the Word documentdocument.save(var_docx_file) import knime.scripting.io as knio# created with the help of: https://chat.openai.com/chatimport docximport openpyxlfrom openpyxl import load_workbookimport osfrom PIL import Image, ImageDraw, ImageFontvar_excel_file = knio.flow_variables['File path']var_docx_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + "_image.docx"var_jpg_file = knio.flow_variables['Parent folder'] + os.sep + knio.flow_variables['File name'] + ".jpg"# Create a new Word documentdocument = docx.Document()# Open the Excel filewb = load_workbook(var_excel_file)# Select the sheet with the tablews = wb['default_1']# you might have to edit thisrange = ws['A1:E151']# Create an image from the rangeimg = Image.new('RGB', (len(range[0])*100, len(range)*20))draw = ImageDraw.Draw(img)# Set the font to use for the cell valuesfont = ImageFont.truetype('Arial', 14)# Iterate through the cells in the range and draw them onto the imagefor row in range: for cell in row: x = cell.column - 1 y = cell.row - 1 # Set the cell's background color to white if the fgColor attribute is not set or is invalid if cell.fill.fgColor is None or cell.fill.fgColor.rgb == '00000000': color = (255, 255, 255) else: color = cell.fill.fgColor.rgb draw.rectangle((x*100, y*20, x*100+99, y*20+19), fill=color) # Add the cell's value with the correct font and size draw.text((x*100+5, y*20+5), str(cell.value), font=font, fill=(0,0,0))# Save the image to a JPG fileimg.save(var_jpg_file , 'JPEG')# Insert the image into the Word documentdocument.add_picture(var_jpg_file)# Save the Word documentdocument.save(var_docx_file) createDummy DatacreateExcel Filescreate4 dummyexcel filesENDCreate Excel Filescreate theExcel File NameslistExcel Filesfrom/data/ folderthat start with the Name"Cluster*"extractFile NamesSTARTto writeto single excelFileENDXLSX -> DOCXas Textpy39_knime_docxXLSX -> DOCXas JPGData Generator Group Loop Start Excel Writer Variable Loop End Create File/FolderVariables List Files/Folders Path to URI URL to File Path Table Row ToVariable Loop Start Variable Loop End Python Script Conda EnvironmentPropagation Python Script Merge Variables

Nodes

Extensions

Links