The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Python
 Operation failed: Book/Sheet type can only be 'w'(
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

BipulKrMahato

Singapore
1 Posts

Posted - 07/10/2025 :  07:21:18 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I am using Origin Lab Pro 2025.
Using the following .py code to copy data from a book/sheet/col to append in another book/sheet/col.

import originpro as op
import numpy as np

def copy_append_data():
try:
# Set the active window to avoid context issues
op.set_show(True)

# Get source data - using absolute references
src_book = op.find_book('Book1')
if src_book is None:
raise Exception("Source Book1 not found. Please ensure it exists and is open.")

# Explicitly get the first sheet if name doesn't work
src_sheet = src_book[0] if src_book.sheets_count >= 1 else None
if src_sheet is None:
raise Exception("No sheets found in Book1")

# Get source data as numpy arrays
colA = np.array(src_sheet.to_list(0), dtype=float)
colB = np.array(src_sheet.to_list(1), dtype=float)

if len(colA) == 0 or len(colB) == 0:
raise Exception("No data found in source columns")

# Handle destination book - critical section
dest_book = op.find_book('Book3')
if dest_book is None:
# Create new book with explicit worksheet type
dest_book = op.new_book('w')
dest_book.lname = 'Book3' # Set name after creation

# Get or create destination sheet
if dest_book.sheets_count == 0:
dest_sheet = dest_book.add_sheet('Sheet1')
else:
dest_sheet = dest_book[0] # Get first sheet

# Get existing destination data
dest_colA = []
dest_colB = []
try:
dest_colA = dest_sheet.to_list(0)
dest_colB = dest_sheet.to_list(1)
except:
pass # Columns might be empty

# Calculate starting row for append
start_row = len(dest_colA) if dest_colA else 0

# Prepare data for insertion
new_data = np.column_stack((colA, colB))

# Insert data in one operation (more efficient)
if start_row == 0:
# First time writing to empty sheet
dest_sheet.from_np(new_data, False)
else:
# Append to existing data
combined = np.vstack((np.column_stack((dest_colA, dest_colB)), new_data))
dest_sheet.from_np(combined, False)

# Set column names
dest_sheet.cols = 2 # Ensure we have 2 columns
dest_sheet.set_label(0, "A")
dest_sheet.set_label(1, "B")

print(f"Success! Appended {len(colA)} rows to Book3/Sheet1")
return True

except Exception as e:
print(f"Operation failed: {str(e)}")
print("Troubleshooting tips:")
print("1. Ensure Book1 exists and has data in columns A-B")
print("2. Close and reopen Origin if you've recently deleted Book3")
print("3. Try running the script with Origin in focus")
return False

# Execute
if __name__ == "__main__":
copy_append_data()

While executing, I am getting the following error -

Operation failed: Book/Sheet type can only be 'w'(Worksheet) or 'm'(Matrix)
Troubleshooting tips:
1. Ensure Book1 exists and has data in columns A-B
2. Close and reopen Origin if you've recently deleted Book3
3. Try running the script with Origin in focus

I followed the suggestions but still getting same error.
Please, help to solve the problem ?

Bipul

YimingChen

1666 Posts

Posted - 07/10/2025 :  09:25:18 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Change
dest_book = op.find_book('Book3')

to
dest_book = op.find_book('w','Book3')


I hope the help document below helps you debug the code.
https://docs.originlab.com/originpro/annotated.html

Thank you
Yiming
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000