How to copy grades' categories from a course to another?

I use the same categories as grade structure for many courses in my institution. And I know how to create categories inside each course by hand. But, I am interesting in a way to copy categories from one course to another, or even better a way to create categories programmatically.

 

Thanks in advance.

 

Screenshot from "Manage Grades" with a sample with categories in a course.

 

Categories