以下是一个示例脚本:

import os
import sys
import pathlib
import json
from contextlib import redirect_stderr
from fontTools import ttLib
from fontmeta import FontMeta

# Check for commandline argument
if len(sys.argv) == 1:
    print('No argument was supplied.')
    exit(0)

fontfile = sys.argv[1]


def font_name(font_path, name_idx):
    font = ttLib.TTFont(font_path, ignoreDecompileErrors=True)
    with redirect_stderr(None):
        names = font['name'].names

    details = {}
    for x in names:
        if x.langID == 0 or x.langID == 1033:
            try:
                details[x.nameID] = x.toUnicode()
            except UnicodeDecodeError:
                details[x.nameID] = x.string.decode(errors='ignore')
    # details[4] = Full Name
    # details[1] = Family Name
    # details[2] = Style Name
    return details[name_idx]


meta_instance = FontMeta(fontfile)

metadata = meta_instance.get_full_data()

fontFullName           = font_name(fontfile,4)
fontFamily             = font_name(fontfile,1)
fontStyle              = font_name(fontfile,2)
fontVers               = metadata[5]['value'];
fontVers               = fontVers.replace('Version ',"v")
fontLang               = metadata[1]['language']['value'];
fontUniqueID           = metadata[3]['value']
fontPostscriptName     = metadata[6]['value']
fontPostscriptEncoding = metadata[6]['encoding']['value']
fontDesigner           = metadata[9]['value']
fontLicenseURL         = metadata[14]['value']

print('Full Name:     ' + fontFullName)
print('Family:        ' + fontFamily)
print('Style:         ' + fontStyle)
print('Version:       ' + fontVers)
print('Language:      ' + fontLang)
print('UniqueID:      ' + fontUniqueID)
print('License URL:   ' + fontLicenseURL)
print('Font Designer: ' + fontDesigner)

Output:

Full Name:     Sharp Sans Bold
Family:        Sharp Sans
Style:         Bold
Version:       v1.001
Language:      English/United States
UniqueID:      1.001;2016;SHRP;SharpSans-Bold
License URL:   http://www.sharptype.co
Font Designer: Lucas Sharp

PS1:

& "D:\Dev\Python\00 VENV\FontTools\Scripts\Activate.ps1"

$val = python "D:\Dev\Python\Font Scripts\GetFontInfo.py" "D:\Fonts\00 Test\SharpSans-Bold.otf"

Write-Host "`$val:" $val -ForegroundColor Green

现在,Python代码只是打印值.我的ps脚本将打印的值作为字符串 echo .除了打印之外,还有什么方法可以将这些值传递给PowerShell--即以数组形式?

或者,我应该返回JSON并在PowerShell中解析它吗?

如果有任何帮助,我很感激.

推荐答案

When PowerShell invokes an external program, it streams the individual lines of its stdout output one by one美元用于PowerShell的流水线.

如果你通过assignment to a variable收看这条视频:

  • two or more行变成存储行的array(类型为[object[]]).

  • one行按原样存储,存储为string(类型[string]).

因此:

$val = python "D:\Dev\Python\Font Scripts\GetFontInfo.py" "D:\Fonts\00 Test\SharpSans-Bold.otf"

by itself is sufficient to capture the stdout output lines from your 100 call as an array - assuming there are two or more lines.

如果你想做ensure that 100 is always an array - even if there's only one output line,你有两个 Select :

  • 任一:使用101, the 100:
$val = @(python "D:\Dev\Python\Font Scripts\GetFontInfo.py" "D:\Fonts\00 Test\SharpSans-Bold.otf")
  • 或者:使用100 type constraint(相当于[object[]]):
[array] $val = python "D:\Dev\Python\Font Scripts\GetFontInfo.py" "D:\Fonts\00 Test\SharpSans-Bold.otf"

Note:

  • 以上两种方法之间有细微的差别--详情请参见this answer.

  • 当PowerShell捕获或重定向来自外部程序的输出时,总是需要根据[Console]::OutputEncoding中存储的字符编码将其解码为.NET字符串-请参见this answer.

  • 要验证$val是否确实是一个数组,请运行$val.GetType().FullName.

  • 要在自己的行上打印每个数组元素,Do not使用Write-Host,只需提交$val on its own(这等同于使用Write-Output $val,尽管后者很少需要显式使用-有关背景信息,请参见this answer).

Python相关问答推荐

Pandas或pyspark跨越列创建

将列中的滚动值集转换为单元格中的单个值

无法获得指数曲线_fit来处理日期

Plotly Dash函数来切换图形参数-pPython

带有Postgres的Flask-Data在调用少量API后崩溃

为什么我的代码会进入无限循环?

Pandas使用过滤器映射多列

Polars Select 多个元素产品

使用argsorted索引子集索引数组

如何在PIL、Python中对图像应用彩色面膜?

按照行主要蛇扫描顺序对点列表进行排序

将HLS纳入媒体包

Python 3.12中的通用[T]类方法隐式类型检索

重新匹配{ }中包含的文本,其中文本可能包含{{var}

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

如果条件不满足,我如何获得掩码的第一个索引并获得None?

driver. find_element无法通过class_name找到元素'""

如何在Python中使用另一个数据框更改列值(列表)

在Python中计算连续天数

跳过嵌套JSON中的级别并转换为Pandas Rame