And even then, the principle remains that a heretic cannot hold any ecclesiastical office. So, the election of a heretic would still be invalid.
This is a gross generalization, as usual.
a) There are many different levels of heresy. Not all preclude one from holding office.
b) There are different aspects to an election and an office. Temporal and spiritual.
c) The church can change human/temporal rules/laws. This would affect your "principle".
Using the examples of Pope Pius X and XII...they DIRECTLY contradict your overly-general principle. So either a) they are both stupid or b) your understanding of the principle is faulty. I'd bet on the latter, especially considering that they were "guided by the Holy Ghost" and can't err, while you can.
It's amazing to me that some of you sedes will yell and scream "the Church cannot err" and the "pope is guided by the Holy Ghost" but when you read the plain english of Pius X and XII on the conclave, you try to say they were wrong or they can't contradict cuм Ex or some other silly explanation.