It is crucial to grasp the characteristics of tumour immune microenvironment to improve effects of immunotherapy. In this study, the immune and stromal scores of 371 cases were calculated for quantitative analysis of immune and stromal cell infiltration in the tumour microenvironment of hepatocellular carcinoma (HCC). The weighted gene co-expression network analysis and protein–protein interaction network were analysed to identify immune microenvironment-related genes. The results showed that patients with high immune scores had a higher 4-year recurrence-free rate. TP53, CTNNB1, and AXIN1 mutations significantly varied with immune scores. In immune score-related modules analysis, Kyoto encyclopaedia of genes and genomes pathways and gene ontology terms were closely related to immune processes, tumorigenesis, and metastasis. Twelve new immune microenvironment-related genes were identified and had significantly positive correlations with seven immune checkpoint genes. In prognostic analysis, eleven immune microenvironment-related genes exhibited high expression, nine of which were validated in the GSE62232 dataset and were significantly associated with a good prognosis. Our findings suggest that calculating immune score and stromal score could help to determine tumour purity and immune cell infiltration in the tumour microenvironment. Nine immune microenvironment-related genes identified in this study had potential as prognostic markers for HCC.