Almost 20,000 pupils in Scotland missed more than half of their lessons last year, a report has found.