Author |
Topic  |
|
pavi-chem
United Kingdom
26 Posts |
Posted - 07/26/2023 : 08:53:36 AM
|
OriginPro 2022b (64-bit) SR1 9.9.5.171 (Academic) Copyright © 1991-2022 OriginLab Corporation Operating System: Windows 10
Hi all,
I am trying to write a code to add more graphs to an existing origin file but the code has problem saving the file after the data is added. How to over come this issue?
import originpro as op
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
import os
import sys
import shutil
def origin_shutdown_exception_hook(exctype, value, traceback):
"""Ensures Origin gets shut down if an uncaught exception"""
op.exit()
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = origin_shutdown_exception_hook
# Only run if external Python
if op.oext:
op.set_show(True)
try:
# Prompt user to select an existing Origin project file
root = tk.Tk()
root.withdraw()
origin_file_path = filedialog.askopenfilename(title="Select Existing Origin Project File",
filetypes=[("Origin Project Files", "*.opju")])
# Load the existing Origin project file
op.open(origin_file_path)
# Add more graphs
more_graphs = True
graph_num = 1
while more_graphs:
# Prompt user to select an Origin template file for the graph
template_file_path = filedialog.askopenfilename(title=f"Select Origin Template File for Graph {graph_num}",
filetypes=[("Origin Template Files", "*.otpu")])
# Prompt user to select an XLSX file for the graph
xlsx_file_path = filedialog.askopenfilename(
title=f"Select XLSX File for {os.path.basename(template_file_path)}",
filetypes=[("XLSX Files", "*.xlsx")])
# Load the XLSX file into a new worksheet
wks = op.new_sheet()
wks.from_file(xlsx_file_path, False)
# Create a graph page with the user-selected template for the graph
gr = op.new_graph(template=template_file_path)
gl = gr[0]
# Prompt user for the range of columns to plot for the graph
column_range = simpledialog.askstring("Range of Columns",
f"Enter the range of columns (e.g., 1-12) for Graph {graph_num}:")
start_col, end_col = map(int, column_range.split("-"))
y_columns = list(range(start_col, end_col + 1))
for col in y_columns:
plot = gl.add_plot(wks, col, 0)
# Group and Rescale the graph
gl.group()
gl.rescale()
# Prompt user if they want to add more graphs
response = tk.messagebox.askyesno("Add More Graphs", "Do you want to add more graphs?")
if response:
graph_num += 1
else:
more_graphs = False
# Tile all windows
op.lt_exec('win-s T')
# Save the modified project to a temporary file
if op.oext:
temp_file_path = os.path.join("H:\\FTIR_data\\DOCUMENTS\\Py-DataProcess\\temp-path", "temp.opju")
op.save(temp_file_path)
# Copy the temporary file back to the original file location
shutil.copy2(temp_file_path, origin_file_path)
os.remove(temp_file_path) # Delete the temporary file after copying
messagebox.showinfo("Operation Completed", "Graphs added successfully to the Origin project file!")
except Exception as e:
print(f"An error occurred: {str(e)}")
op.exit() The process cannot access the file because it is being used by another process: 'H:\\FTIR_data\\DOCUMENTS\\Py-DataProcess\\temp-path\\temp.opju'
Code added the new data and graphs, but it seems like it has trouble saving the file and I see that it looks like a read only file. any idea how to add data to existing files without this issue? any help/advice.
Thanks a bunch! Pavi. |
Edited by - pavi-chem on 07/26/2023 08:56:30 AM |
|
YimingChen
1643 Posts |
Posted - 07/27/2023 : 2:32:25 PM
|
why don't you save the project to overwrite the original file? Also, if you save the project file from menu File->Save Project, will it show message that it's read-only?
James |
 |
|
pavi-chem
United Kingdom
26 Posts |
Posted - 07/28/2023 : 12:23:46 PM
|
Yes, The files shows as read-only. How to overwrite the original project and save the file without read-only situation?
Thanks a bunch! Pavi. |
 |
|
YimingChen
1643 Posts |
Posted - 07/28/2023 : 5:12:59 PM
|
It could happen when the file is opened from multiple Origin instances at the same time. Can you first save it as a new project file and try the code again? Thanks.
|
 |
|
pavi-chem
United Kingdom
26 Posts |
Posted - 08/07/2023 : 06:01:09 AM
|
Hi,
I made sure that the project was saved properly and tried to overwrite the original file but getting same error as below picture.
import sys
import originpro as op
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
import os
def origin_shutdown_exception_hook(exctype, value, traceback):
"""Ensures Origin gets shut down if an uncaught exception"""
op.exit()
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = origin_shutdown_exception_hook
# Only run if external Python
if op.oext:
op.set_show(True)
try:
# Prompt user to select an existing Origin project file
root = tk.Tk()
root.withdraw()
origin_file_path = filedialog.askopenfilename(title="Select Existing Origin Project File",
filetypes=[("Origin Project Files", "*.opju")])
# Load the existing Origin project file
op.open(origin_file_path)
# Add more graphs
more_graphs = True
graph_num = 1
while more_graphs:
# Prompt user to select an Origin template file for the graph
template_file_path = filedialog.askopenfilename(title=f"Select Origin Template File for Graph {graph_num}",
filetypes=[("Origin Template Files", "*.otpu")])
# Prompt user to select an XLSX file for the graph
xlsx_file_path = filedialog.askopenfilename(
title=f"Select XLSX File for {os.path.basename(template_file_path)}",
filetypes=[("XLSX Files", "*.xlsx")])
# Load the XLSX file into a new worksheet
wks = op.new_sheet()
wks.from_file(xlsx_file_path, False)
# Create a graph page with the user-selected template for the graph
gr = op.new_graph(template=template_file_path)
gl = gr[0]
# Prompt user for the range of columns to plot for the graph
column_range = simpledialog.askstring("Range of Columns",
f"Enter the range of columns (e.g., 1-12) for Graph {graph_num}:")
start_col, end_col = map(int, column_range.split("-"))
y_columns = list(range(start_col, end_col + 1))
for col in y_columns:
plot = gl.add_plot(wks, col, 0)
# Group and Rescale the graph
gl.group()
gl.rescale()
# Prompt user if they want to add more graphs
response = tk.messagebox.askyesno("Add More Graphs", "Do you want to add more graphs?")
if response:
graph_num += 1
else:
more_graphs = False
# Tile all windows
op.lt_exec('win-s T')
# Try to save the project file
try:
op.save(origin_file_path)
except Exception as e:
print(f"An error occurred: {str(e)}")
# If the save fails, show an error message
messagebox.showerror("Error Saving Project", f"An error occurred while saving the project file: {str(e)}")
except Exception as e:
print(f"An error occurred: {str(e)}")
op.exit()

how do I add new graphs to existing origin file and save it?
Thanks a bunch! Pavi. |
 |
|
YimingChen
1643 Posts |
Posted - 08/07/2023 : 09:07:24 AM
|
The file path is not valid.
|
 |
|
pavi-chem
United Kingdom
26 Posts |
Posted - 08/09/2023 : 11:05:54 AM
|
Hi,
Thanks for pointing that out!
output_file_path = os.path.abspath(origin_file_path)
op.save(output_file_path)
Added this and it fixed it. :)
Thanks a bunch! Pavi. |
 |
|
|
Topic  |
|
|
|