Icon

kn_​example_​python_​excel_​import_​export_​openpyxl

use Python Pandas and Openpyxl to import and export Excel (sheets) and Export them back - also append to an existing Excel file

use Python Pandas and Openpyxl to import and export Excel (sheets) and Export them back - also append to an existing Excel file

import knime.scripting.io as knioimport pandas as pdimport openpyxl# Read input table from KNIMEdf = knio.input_tables[0].to_pandas()# Flow variables (Excel file paths)excel_file_path = knio.flow_variables['var_excel2_file'] # destination Excel file# Load the existing workbookwb = openpyxl.load_workbook(excel_file_path)# Select the existing sheet named "Sheet"if "Sheet" in wb.sheetnames: ws = wb["Sheet"]else: # Optional: create the sheet if it doesn't exist ws = wb.create_sheet("Sheet")# Find the first empty row to start appendingstart_row = ws.max_row + 1# Append the DataFrame rows (excluding header)for row in df.itertuples(index=False, name=None): ws.append(row)# Save the workbookwb.save(excel_file_path) use Python Pandas and Openpyxl to import and export Excel (sheets) and Export them back - also append to an existing Excel file KNIME and Excel — An Overview of What You Can Dohttps://medium.com/p/c6d400319745KNIME — Python Overviewhttps://medium.com/p/aa4f3a55a768 Create an empty Excel File to append the data later locate and create/data/ folderwith absolute paths/data/ subfolderfile_01.xlsx^(var_excel_file|var_excel2_file)$var_excel_filev_excel_pathvar_excel2_fileENDfile_02.xlsxdelete file_02.xlsxTry todeletefile_02.xlsxTry todeletefile_02.xlsxdf = pd.read_excel(knio.flow_variables['v_excel_path'], sheet_name=knio.flow_variables['Sheet'], engine='openpyxl')=> import Excel sheet with openpyxlSTARTAppend data to an existingExcel Sheetsheet.append()file_01.xlsxcreate empty valuefile_02.xlsxSheetfile_02.xlsxCollect LocalMetadata Generate Excel File Read ExcelSheet Names String to Path(Variable) Java EditVariable (simple) ConstantValue Column Java EditVariable (simple) Variable Loop End Read ExcelSheet Names DeleteFiles/Folders Try (VariablePorts) Catch Errors(Var Ports) Merge Variables Python Script Table Row ToVariable Loop Start Python Script Excel Reader Row Filter Excel Writer Excel Reader import knime.scripting.io as knioimport pandas as pdimport openpyxl# Read input table from KNIMEdf = knio.input_tables[0].to_pandas()# Flow variables (Excel file paths)excel_file_path = knio.flow_variables['var_excel2_file'] # destination Excel file# Load the existing workbookwb = openpyxl.load_workbook(excel_file_path)# Select the existing sheet named "Sheet"if "Sheet" in wb.sheetnames: ws = wb["Sheet"]else: # Optional: create the sheet if it doesn't exist ws = wb.create_sheet("Sheet")# Find the first empty row to start appendingstart_row = ws.max_row + 1# Append the DataFrame rows (excluding header)for row in df.itertuples(index=False, name=None): ws.append(row)# Save the workbookwb.save(excel_file_path) use Python Pandas and Openpyxl to import and export Excel (sheets) and Export them back - also append to an existing Excel file KNIME and Excel — An Overview of What You Can Dohttps://medium.com/p/c6d400319745KNIME — Python Overviewhttps://medium.com/p/aa4f3a55a768 Create an empty Excel File to append the data later locate and create/data/ folderwith absolute paths/data/ subfolderfile_01.xlsx^(var_excel_file|var_excel2_file)$var_excel_filev_excel_pathvar_excel2_fileENDfile_02.xlsxdelete file_02.xlsxTry todeletefile_02.xlsxTry todeletefile_02.xlsxdf = pd.read_excel(knio.flow_variables['v_excel_path'], sheet_name=knio.flow_variables['Sheet'], engine='openpyxl')=> import Excel sheet with openpyxlSTARTAppend data to an existingExcel Sheetsheet.append()file_01.xlsxcreate empty valuefile_02.xlsxSheetfile_02.xlsxCollect LocalMetadata Generate Excel File Read ExcelSheet Names String to Path(Variable) Java EditVariable (simple) ConstantValue Column Java EditVariable (simple) Variable Loop End Read ExcelSheet Names DeleteFiles/Folders Try (VariablePorts) Catch Errors(Var Ports) Merge Variables Python Script Table Row ToVariable Loop Start Python Script Excel Reader Row Filter Excel Writer Excel Reader

Nodes

Extensions

Links