Just how to get Bitcoin Addresses in (P2WPKH) format from ripemd160 hash?
You need the public key in order to generate P2PKH or P2WPKH. So if the address is spent from, or signed with, then you can generate the corresponding P2WPKH.
No public key - no way.
He's trying to get the bech32 address from a ripemd160 hash not the whole script. It's definitely possible, because the address is just an encoding of the hash into bech32 format.
It is also possible to get the base58 address from a ripemd160 (otherwise we wouldn't be able to generate addresses!).
For base58 we just put 0x00 at the beginning of the ripemd160 hash to signal that it's a mainnet address. We copy this byte array and then it's hashed with SHA256(SHA256(x)) , so a double hash. The last 4 bytes of that are appended to the ripemd160 as a checksum. Then it's encoded in base58. So we have:
1 byte network indicator (0x00 mainnet)
20 bytes ripemd160 hash
4 bytes sha256(sha256([network indicator + ripemd160]))
import hashlib
import base58
# (h is hashlib ripemd160 hash object)
# Add 00 for 'main network'
rh = bytearray("\x00", "ascii") + h.digest()
# Hash it twice
eh = hashlib.sha256(rh)
eh2 = hashlib.sha256(eh.digest())
# Last 4 bytes of the double hashed RIPEMD-160 hash (incl. prefix) are the checksum
checksum = eh2.digest()[:4]
# Add checksum to RIPEMD160 extended hash
rh += checksum
address = base58.b58encode(bytes(rh))
bech32 is slightly more complicated. For that we not only need the ripemd160 hash, we also need the witness version byte, which is currently fixed at 0. We also need to pass the network type which in the case for segwit's is just "bc" (yes, bc, NOT bc1 or bc1q). Then pass these three pieces of data to Pieter Wuille's
encode() function as described at
https://bitcoin.stackexchange.com/questions/70507/how-to-create-a-bech32-address-from-a-public-key .