Put it this way - no one really ‘excommunicates’ anyone else in Catholicism. A person excommunicates themselves (puts themselves out of communion with the Church) by their actions. All anyone else can do is ‘declare’ publicly that that person is excommunicated. It’s a subtle difference.
And some sins, for example abortion, bring automatic excommunication upon the sinner without ANYONE having to declare it. So a priest who tells you that you are excommunicated by reason of your mortal sin of having had an abortion is correct, and entitled to say so, but they’re not actually excommunicating you.