安装和介绍
可以用pip安装Python/WinRT,这样就可以在Python中访问WinRT的API了。
Python/WinRT要求Python3.7以上,以及Windows 1809以后的版本。
Python/WinRT是xlang的一个附属项目,具有比较强的试验性质。微软在Get started using Python on Windows for scripting and automation中有提及使用Python/WinRT做WinRT的Automation。
一个使用Python/WinRT的例子是WinML Tutorial。
一些例子
生成随机16进制字符串
>>> from winrt.windows.security.cryptography import CryptographicBuffer
>>> CryptographicBuffer.generate_random_number()
1259899221
>>> random_seq = CryptographicBuffer.generate_random(8)
>>> random_seq
<_winrt_Windows_Storage_Streams.IBuffer object at 0x000001C6031E6330>
>>> CryptographicBuffer.encode_to_hex_string(random_seq)
'59f61f2adf075d5c'
编码hello
>>> from winrt.windows.security.cryptography import BinaryStringEncoding
>>> BinaryStringEncoding
<enum 'BinaryStringEncoding'>
>>> dir(BinaryStringEncoding)
['UTF16_B_E', 'UTF16_L_E', 'UTF8', '__class__', '__doc__', '__members__', '__module__']
>>> str_bin = CryptographicBuffer.convert_string_to_binary("hello", BinaryStringEncoding.UTF8)
>>> CryptographicBuffer.encode_to_hex_string(str_bin)
'68656c6c6f'
>>> CryptographicBuffer.encode_to_base64_string(str_bin)
'aGVsbG8='
(本篇完)
2020-07-23 封箱拆箱值
from winrt.windows.foundation import IPropertyValue
from winrt.windows.foundation import PropertyValue
boxed = PropertyValue.create_string("hello")
IPropertyValue._from(boxed).get_string()
boxed = PropertyValue.create_uint8_array(list(b'asdb'))
参考
(更新完)