If the data files you wish to import cannot be imported via the Import Filters ASCII or Binary files options, you can write your own Python or Origin C function to import the files.
Learn about how to create a Python-based Import Filter. |
Python code can be used to import a user-defined data file. A rough example of such code is presented below. The most important point about this code is that the target worksheet will be activated prior to the code being called by Origin. Additionally, the fname$ LabTalk variable will be populated with the full path and file name of the current file to import. Please read the code comments to better understand what needs to be done in the Python code.
import originpro as op import pandas as pd # Function reads file into a pandas DataFrame. def read_file(file): df = pd.DataFrame() with open(file, 'r') as f: line = f.readline() while line: # Put your code here to parse the line in the file # and then append it to the DataFrame. return df # The file chosen by Origin import filter is placed into the fname$ # LabTalk variable. Must bring it into Python. Don't specify $ in name! fname = op.get_lt_str('fname') # Read the file into a pandas DataFrame. data = read_file(fname) # Returns the active sheet. wks = op.find_sheet() # Add DataFrame to sheet starting at first column. # DataFrame column labels will become worksheet column long names. # Origin automagically sets column format based on DataFrame data types. wks.from_df(data)
After creating the code to handle the importing, you can use it as follows:
| *The target window template named on the Source page of the Import Wizard is only used when new windows are created (as would happen under some conditions during drag-and-drop importing). If choosing Data: Import from File from the menu and your active window is consistent with your import filter's Target Window specification, no new window is created and a reference to the page object for the active window is passed to your function. If the active window is of a different type, a new window will be created using the specified template, and the page reference to this new window is passed. |
You can develop your own Origin C function to handle the importing user-defined data files by creating an Origin C function in an Origin C file.
The prototype of Origin C the function must be either one of the following:
| Note: More details on how to write such Origin C functions can be found in the Importing Data chapter in Origin C Guide. |
After writing a function to handle the importing and saving it to an Origin C file, you can use it as follows:
| *The target window template named on the Source page of the Import Wizard is only used when new windows are created (as would happen under some conditions during drag-and-drop importing). If choosing Data: Import from File from the menu and your active window is consistent with your import filter's Target Window specification, no new window is created and a reference to the page object for the active window is passed to your function. If the active window is of a different type, a new window will be created using the specified template, and the page reference to this new window is passed. |