Rambam (MT, Torah study, beg. Ch. 4) states:
וְכֵן הָרַב שֶׁאֵינוֹ הוֹלֵךְ בְּדֶרֶךְ טוֹבָה אַף עַל פִּי שֶׁחָכָם גָּדוֹל הוּא וְכָל הָעָם צְרִיכִין לוֹ אֵין מִתְלַמְּדִין מִמֶּנּוּ עַד שׁוּבוֹ לַמּוּטָב. שֶׁנֶּאֱמַר (מלאכי ב ז) "כִּי שִׂפְתֵי כֹהֵן יִשְׁמְרוּ דַעַת וְתוֹרָה יְבַקְשׁוּ מִפִּיהוּ כִּי מַלְאַךְ ה' צְבָאוֹת הוּא". אָמְרוּ חֲכָמִים אִם הָרַב דּוֹמֶה לְמַלְאַךְ ה' צְבָאוֹת תּוֹרָה יְבַקְּשׁוּ מִפִּיהוּ אִם לָאו אַל יְבַקְּשׁוּ תּוֹרָה מִפִּיהוּ
Trans. (Sefaria):
Likewise, a Rabbi who does not follow the good way, though he be a great scholar and all of the people stand in need of him no instruction should be received from him until his return to goodness, as it is said: "For the priests' lips should keep knowledge and they should seek the Law at his mouth; for he is the messenger of the Lord of Hosts" (Mal. 2.7). Thereupon the sages said: "If the Rabbi be like unto the messenger of the Lord of Hosts then shall they seek the Law at his mouth, if not, they shall not seek the Law at his mouth.
However, R. Yirmiyahu Lew in his commentary to Rambam, Divrei Yirmiahu (loc. cit.) brings his father's opinion that learning from such a teacher's books would be permitted as one is able to "take the good and leave the bad", as we see from Rambam himself who studies books of deplorable people.