I know the Catholic Church excommunicates Catholics who join Freemasonic lodges, as well as Catholics who the Church judges to be willful heretics. But has the Church ever excommunicated a Catholic purely for being a member of a Communist or National Socialist party? The Church has never excommunicated Adolf Hitler, and some other Catholics who belong to Marxists groups, so I'm curious.
The Church has technically never reversed the decree excommunicating communists.
However, it is no longer common practice to excommunicate individuals based on their political beliefs, and even directly after the decree was made, it was rarely enforced. Membership in Communist organizations or holding communist political beliefs is still considered a heterodoxy because it has been explicitly condemned by the Church, but it is no longer considered grounds for excommunication.
By the way, there were bishops who were excommunicated because they cooperated with the communists. You can read more about Chineses bishops.
Has the Catholic Church ever excommunicated someone based on their ideology?
The short answer is yes. The Roman Catholic Church has excommunicated members of her faithful based on their ideologies!
The Church has excommunicated both Communists and Nazis.
The Decree Against Communism was a 1949 Catholic Church document issued by the Supreme Sacred Congregation of the Holy Office, and approved by Pope Pius XII, which declared Catholics who professed Communist doctrine to be excommunicated as apostates from the Christian faith.
Opposition to Socialism and Communism in Catholic social teaching had already been expressed in the teachings of popes since the encyclicals Nostis et nobiscum (1849), Quanta cura (1864), and Rerum novarum (1891).
In the earlier social encyclicals the criticism of Communism described it as a system violating human rights: e.g., the right to own property. After revolutions in Russia, China, and Mexico had been followed by religious persecution, a new theme of criticism was added, beginning with Quadragesimo anno (1931) by Pope Pius XI. This encyclical objected to what it considered to be Communism's professed opposition to religion, and its threat to the freedom and the very existence of the Church. In 1937, Pius XI rejected atheistic communism in an encyclical entitled Divini Redemptoris as "a system full of errors and sophisms", with a "pseudo-ideal of justice, equality, and fraternity" and "a certain false mysticism", and contrasted it with a humane society (civitas humana).
After the Italian parliamentary election of April 1948, in which the Communist-Socialist coalition won 31% of the vote, the Holy Office began to study the issue of Communism in order to give guidance to Catholic lay people and clergy with questions about support for Communist parties.
An additional impulse for Vatican action against Communism arose in Czechoslovakia, where the Communist government, installed by a coup d'état in February 1948, undertook a campaign to take control of the Catholic Church by several means. Among other measures, it created an organization of priests favorable to the regime, took control of church finances, and demanded that pastoral letters to the faithful or the clergy be approved by government ministries.
On July 15, 1948, L'Osservatore Romano published a decree which excommunicated those who propagate "the materialistic and anti-Christian teachings of Communism". The document, however, did not mention the Italian Communist Party, which had changed its statutes in 1946, removing an explicit profession of Marxism-Leninism, and opening to participation by citizens, "independent of race, religious faith or philosophical convictions".
In the spring of 1949, pressure on the Church in Czechoslovakia was increasing, and, according to Cardinal Giovanni Battista Montini, then papal Secretary of State, Pope Pius XII had come to feel that there would be no effective diplomatic opposition from the West. Thus the Church had to use what means it had to confront Communism, not only in the immediate situation, but for a long-term opposition.
Form of the document
The document, as published in the Acta Apostolicae Sedis, bears the date July 1, 1949 and the heading Decretum (Decree), and is presented in the form of a dubium: that is, in question-and-answer format. It presents four questions, together with the Holy Office's replies: (1) Is it licit to join or show favor to Communist parties? (2) Is it licit to publish, distribute, or read publications that support Communist doctrine or activity, or to write for them? (3) May Christians who knowingly and freely commit the acts in parts 1 and 2 be given the sacraments? (4) Do Christians who profess, defend or promote materialistic Communist doctrine incur the penalty of excommunication as apostates from the Christian faith, with the penalty reserved so that it may only be lifted by the Holy See?
The answers in the decree were negative to the first three questions and affirmative to the fourth.
Although not named, Adolf Hitler has been formally excommunicated from the Church because he was an active member of the Nazi Party!
February 1931, the German bishops meeting in conferences in Fulda and Freising supported the action of the bishop of Mainz, but limited the excommunications to Nazi leaders and activists.
In February 1931, the German bishops excommunicated all active Nazi party members. This included Hitler. This penalty was not imposed on those who merely voted Nazi. It was hoped to persuade them by argument. The same policy was in operation against the Communists. - The Catholic Response to Nazism
Ideologies will come and go. The Church will deal with them according to their doctrine, situation and circumstances at that time in history.