3.3.2 Exporting Origin Worksheet to Excel


This example shows how to use another package, XlsxWriter, to export the active worksheet to an Excel file.

  1. How to ask Origin the User File Folder.
  2. How to get the active worksheet.
  3. How to loop through the columns and get meta data and values.

You can install Python packages from the script window with InstallPythonPackages, or you can install the Package Manager for Python App.


import PyOrigin
#install from Script Window: InstallPythonPackages(0,xlsxwriter)
import xlsxwriter
wks =  PyOrigin.ActiveLayer()
file = PyOrigin.GetPath(PyOrigin.PATHTYPE_USER)+'Data.xlsx'
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})

for i, col in enumerate(wks.Columns()):
	header = col.GetLongName();
	unit = col.GetUnits();
	data = col.GetData()
	worksheet.write(0, i, header, bold)
	worksheet.write(1, i, unit, bold)
	worksheet.write_column(2, i, data)


To run this sample:

  1. Open Code Builder.
  2. Create a new Python file in the User File Folder, like test1.py
  3. After putting in the code above and save the file test1.py, open the script window and execute:
run -pyf test1.py