It's possible.
You can refer to the following URL to change the paper size for Microsoft Print to PDF:
https://franklinheath.co.uk/2015/08/29/custom-page-sizes-for-microsoft-print-to-pdf/
You can dynamically create a paper size using Python, then retrieve the index number of the created paper size and set it to pageSetup.paperSize.