套打Python插件示例:使用正则表达式提取汉字原创
金蝶云社区-王文亮
王文亮
10人赞赏了该文章 1,085次浏览 未经作者许可,禁止转载编辑于2022年02月16日 09:44:37

需求示例:采购订单打印,打印备注时候只显示备注里面的中文

表单插件示例:

import clr
from System import *
from System.Text.RegularExpressions import *

def OnPrepareNotePrintData(e):
	if e.DataSourceId == 'FPOOrderEntry':
		if e.DynamicObjectType.Properties.ContainsKey('FEntryNote'):
			for index in range(len(e.DataObjects)):
				text = e.DataObjects[index]['FEntryNote'].ToString();
				if len(text)>0:
					result = Regex.Match(text,"[\u4e00-\u9fa5]+").Value;
					e.DataObjects[index]['FEntryNote'] = result;

image.png


有小伙伴反馈上面的正则表达式在原始文本是“中文+英文+中文”时候只能提取第一段中文,期望能提取到所有的中文文本,那么就需要稍微做下改动,示例如下:

import clr
from System import *
from System.Text.RegularExpressions import *

def OnPrepareNotePrintData(e):
    if e.DataSourceId == 'FPOOrderEntry':
        if e.DynamicObjectType.Properties.ContainsKey('FEntryNote'):
            for index in range(len(e.DataObjects)):
                text = e.DataObjects[index]['FEntryNote'].ToString();
                if len(text)>0:
                    Matches = Regex.Matches(text, "[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
                    result='';
                    for m in Matches:
                        result = result + m.Value;
                    e.DataObjects[index]['FEntryNote'] = result;

image.png

赞 10