There are two types of excommunication:
(1) the kind issued as a judgment in a particular case (ferendae sententiae)
(2) the automatic kind (latae sententiae)
(1) The first kind cannot be issued against the Pope, since no higher authority on earth would be able to judge him. So no group of Cardinals or Bishops could get together and excommunicate the Pope. They lack the authority to judge him in the first place.
(2) The second kind is divided into two sub-types:
(a) automatic excommunication specified in canon law, and
(b) automatic excommunication which is inherent to certain types of sins.
(2a) The Roman Pontiff is above Canon Law. Now some parts of canon law are not per se of the law; they are direct expressions of teachings on faith or morals. But the parts that are per se of the law are changeable and dispensable by the Roman Pontiff. So if the Pope wished to do so, he could dispense himself from anything in Canon law that is per se of the law. And the Pope need not change the law first. He can simply disregard the law whenever he wishes. His words and deeds to the contrary of the law constitute, in and of themselves, a dispensation.
However, the second type of the second type (2b), though it is stated in Canon law, is nevertheless of the eternal moral law, and applies even if not stated therein. This type of automatic excommunication applies to only three types of sins: apostasy, heresy, or schism.
If a baptized Christian were to abandon the Christian faith, he would be committing the sin of apostasy, and would be automatically excommunicated. Formal heresy and formal schism each also carry the penalty of automatic excommunication. And since these things are of the eternal moral law, the Pope cannot dispense himself, and he is not above this type of law.
However, the First Vatican Council taught that each Pope has the gift of truth and of a never-failing faith, divinely conferred. Therefore, I conclude that no Pope can teach or commit heresy, nor can any Pope commit apostasy or schism. The prevenient grace of God absolutely prevents it. This work of God’s grace is based on the promises of Jesus, that His Church would be indefectible, and that the faith of Peter would not fail. And it is not contrary to free will, since the Pope freely accepts his office, and he can freely resign.
Saint Robert Bellarmine is often misquoted on this point. Here is What Saint Bellarmine really said about Popes and Heresy. There is also a good book on the subject, excerpts from Bellarmine’s writings, titled: Papal Error?: A Defense of Popes said to have Erred in Faith. God does not permit any Pope to teach or commit heresy, for the sake of the Church and the salvation of souls.
A Pope cannot be excommunicated ferendae sententiae, since no authority can bring that judgment against him. He is exempt from any latae sententiae excommunications that are per se of Church law. And the only type of excommunication left does not apply to him, as God’s grace prevents him from committing the acts that carry that penalty under the eternal moral law.
Therefore, the Roman Pontiff can never be excommunicated.
Please take a look at this list of my books and booklets, and see if any topic interests you.