Leo Chan Posted June 2, 2023 Posted June 2, 2023 when I loads multiple files use ironpython in one go which have the same structure but when a column has the same name and different Data types, the columns get duplicated. how to fix this on import?
Fredrik Rosell Posted June 5, 2023 Posted June 5, 2023 Hello,To start with, could you please share your IronPython script here, and also example files that can be used to reproduce the issue?
Leo Chan Posted June 7, 2023 Author Posted June 7, 2023 Here is script we used:import clrimport osclr.AddReference("Microsoft.Office.Interop.Excel")import Microsoft.Office.Interop.Excel as Excelfrom Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettingsfrom Spotfire.Dxp.Data import AddRowsSettings, DataTableSaveSettings, DataTypefrom Spotfire.Dxp.Framework.ApplicationModel import ApplicationThreadfrom Spotfire.Dxp.Application import DocumentSaveSettingsfrom System.IO import Fileclr.AddReference("System.Windows.Forms")import System.Windows.Forms.DialogResult as DialogResultfrom System.Windows.Forms import FolderBrowserDialog, DialogResultfrom System.Windows.Forms import OpenFileDialog, SaveFileDialogfrom System.Windows.Forms import OpenFileDialogopenFileDialog = OpenFileDialog()openFileDialog.Title = "選擇檔案"openFileDialog.Filter = "Excel 檔案 (*.xlsx)|*.xlsx"openFileDialog.Multiselect = TruedialogResult = openFileDialog.ShowDialog()if dialogResult == DialogResult.OK: file_paths = openFileDialog.FileNames print("選擇的檔案路徑:") for file_path in file_paths: print(file_path)saveFileDialog = SaveFileDialog()saveFileDialog.Title = "儲存檔案"saveFileDialog.Filter = "Spotfire檔案 (*dxp*)|*dxp*"dialogResult2 = saveFileDialog.ShowDialog()if dialogResult2 == DialogResult.OK: file_path = saveFileDialog.FileName print("儲存的檔案路徑:", file_path)# Define the file pathsfolder_path = openFileDialog.FileNamespotname = saveFileDialog.FileName##print(folder_path)##print(spotname)xlsx_files = file_paths# Loop through all the .xlsx files in the folder pathfor xlsxname in xlsx_files: # Open the Excel file excelApp = Excel.ApplicationClass() workbook = excelApp.Workbooks.Open(xlsxname) ii = 1 # Loop through the worksheets in the Excel file for worksheet in workbook.Worksheets: # Rotate the spreadsheets to the beginning of the file worksheet.Move(workbook.Worksheets(1)) workbook.Save() # Check if the data table already exists in the Spotfire document status, table = Document.Data.Tables.TryGetValue(worksheet.Name) # Create a file data source for the worksheet ds = Document.Data.CreateFileDataSource(xlsxname) ds.IsPromptingAllowed = True # If the data table exists, replace it with new data for the first time, else add rows for later iterations if status: if ii == 0: table.ReplaceData(ds) else: table.AddRows(ds, AddRowsSettings(table, ds)) # Add a new data table if it doesn't exist else: myNewTable = Document.Data.Tables.Add(worksheet.Name, ds) settings = DataTableSaveSettings(myNewTable, False, False) Document.Data.SaveSettings.DataTableSettings.Add(settings) # Close the Excel application excelApp.Workbooks.Close() excelApp.Quit()def g(app,fileName,saveSettings): def f(): app.SaveAs(fileName, saveSettings) return f# Set the file namefileName = (spotname)# Executing the function on the application thread, and Save the document back to the LibraryApplication.GetService[ApplicationThread]().InvokeAsynchronously(g(Application, fileName,DocumentSaveSettings()))print("All Done")
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now