Jump to content

Python script to change all measurement check names


---
 Share

Recommended Posts

We occasionally must re-name 100 check names as our engineering dept make complete changes.  We are given a csv file with old and new names in column A and B. Id like to make a script to automate this for GOM programs.  Is there anyway for me to communicate with the .inspect program file and do this?

Link to comment
Share on other sites

Hello Tim

 

If I understand correctly you want to rename a CSV file at export automatically our do you want to change the name on an existing folder with all the CSV files like (file 01, file 02, file 03...))?

 

If the intension is to change the name on an automatic export program like one that make the full scan and transform our file and export it to a destination you need to make a cycle like a While cycle with a count function and the you need to rename all files according to the count 

 

exp:

# import module

import os

# count function and while cycle

count=0

while count<n:

   execute script

   export csv

   source=file_name

   new name = folder + "Name" + str(count) + ".csv"    # need to create the folder="C:/User...."

   os. rename (source,newname)

   count=count+1

If the intension is to rename all existing files of an existing folder

The following code will do the job for us. It traverses through the lists of all the files in folder, defines the destination (dst) and source (src) addresses, and renames using rename module

folder=(r'C:\Users..."
for count, filename in enumerate (os.listdir(folder)):
        dst = f"NAME {str(count)}.csv"
        src =f"{folder}/{filename}"   # foldername/filename, if .py file is outside folder
        dst =f"{folder}/{dst}"
 os.rename(src, dst)
 
 I hope that I could help with this examples 
Edited
Link to comment
Share on other sites

Thanks...I'm actually trying to do the opposite. I want to open an excel file and all the old names are in column A and all the new names are in column B.

Our engineering dept sends us an excel sheet of the new names so if I can interact with GOM I can automate this....

update.....

 

I just figured it out, I used the 'record' button in GOM to generate the function that communicated with the GOM program so I was able to write the below code and it works extremely well and is fast:

 

import gom
import openpyxl

# location of excel file
excel_path = r'C:\Users\user\Desktop\names.xlsx'

# open excel file
wb = openpyxl.load_workbook(excel_path)
ws = wb.active

# for loop from row 1 to last row in excel sheet
for i in range(1, ws.max_row + 1):
	old_name = ws['A' + str(i)].value # col A is old names
	new_name = ws['B' + str(i)].value # col B is new names

	# gom function called that will change the name
	gom.script.sys.rename_elements_by_character_sequence(
		character_sequences={'from': old_name, 'to': new_name},
		elements=[gom.app.project.inspection[old_name]])

	wb.close()

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...