\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{pythontex}
\usepackage{nopageno}
\usepackage{xstring}
\setlength{\parindent}{0pt}
\begin{document}
\begin{pycode}
import random
from sympy import Symbol
from sympy import expand
x=Symbol('x')
print('\\begin{align*}')
for n in range(10):
    a = random.choice([1, -1]) * random.randint(2, 9)
    b = random.choice([1, -1]) * random.randint(2, 9)
    c = random.choice([1, -1]) * random.randint(2, 9)
    d = random.choice([1, -1]) * random.randint(2, 9)
    e = (a*x+b)*(c*x+d)       
    print('\\StrSubstitute{',e,'}{*}{}',' & = \\StrSubstitute{',expand(e),'}{**}{^}[\\result]\\StrSubstitute{\\result}{*}{}\\\\')
print('\\end{align*}')
\end{pycode}        
\end{document}