Possible to copy sheet in excel document from Ansys?

Hello
I have a small python script that writes results to an existing excel document. The script creates a new sheet for each result set in Ansys, and works as intended.

Now I would like (for each result set) to:
1) Copy the first sheet in existing excel document and re-name it
2) Write Ansys data to cells of the copied sheet

In existing excel document there are a few calculations defined, that should be done with new data as input (this can likely be done more efficient using numpy, but as that requires some additional tweaking of workbench (to my understanding) I would like to avoid that, at least for now).

To solve this I have tried with different variants of copy, but not found anything that works. When interacting with excel from Ansys I have mostly taken other examples and slightly modified them, so my knowledge here is rather limited.

Picture schematically of current script and what I am trying to achieve.

Would it be possible to copy a sheet in an existing excel file from Ansys/python?
Best regards,
Erik

Best Answer

  • erik_b
    erik_b Member Posts: 6
    First Comment
    **
    Answer ✓

    I did some tests, based on your suggestions, and variant below seems to work at my end:

    sheet_to_copy = workbook.Worksheets(1) # Copy first sheet in the document
    sheet_to_copy.Copy(After=workbook.Sheets(workbook.Sheets.Count))
    ws = workbook.Sheets(workbook.Sheets.Count) # Copied sheet, placed last in the document

Answers

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 105
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @erik_b This API is not specific to Ansys.

    Here is something you can try:

    ws.Copy(None, workbook.Sheets(workbook.Sheets.Count)) # ws is the sheet you want to copy

    new_ws = workbook.Sheets(workbook.Sheets.Count) # Since we placed at the end of all sheets

  • erik_b
    erik_b Member Posts: 6
    First Comment
    **

    I absolutely understand that doing Excel operations from python script in Ansys is eventually on the limit of the intended use of the software and beyond scope of current forum.
    With that said I of course tried with the Copy-function, but did not get it to work:

    The copy function is not recognized (?), or maybe I have got something wrong here?