学习笔记-Python常用方法整理原创
金蝶云社区-请输入昵称___
请输入昵称___
46人赞赏了该文章 1,447次浏览 未经作者许可,禁止转载编辑于2023年03月13日 09:27:05

Post请求

def ToPost(posturl,postdata,ftype,fcookie):
	result="";
	try:
		webRequest=HttpWebRequest.Create(posturl)
		webRequest.Method="POST"
		if(fcookie<>"" and fcookie!=None):
			webRequest.Headers.Add("Cookie",fcookie)
		#webRequest.Headers.Add("Cookie", "kdservice-sessionid=6e1511ab-2f15-4760-8b19-6b46d422a5e3")	
		if(ftype<>""):
			webRequest.ContentType=ftype
		#webRequest.ContentType="application/x-www-form-urlencoded"
		webRequest.ContentType="application/json;charset=utf-8"
		data=Encoding.UTF8.GetBytes(postdata)
		webRequest.ContentLength=data.Length
		webRequest.GetRequestStream().Write(data,0,data.Length)
		webRequest.GetRequestStream().Flush()
		webRequest.GetRequestStream().Close()
		webResponse=webRequest.GetResponse()
		streamReader=StreamReader(webResponse.GetResponseStream(),Encoding.GetEncoding("utf-8"))
		result=streamReader.ReadToEnd()
		#return result
	except Exception as e:
		result=str(e)+postdata
	finally:
		return result

文件读取

Path=PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, "123456.txt")
sw1=open(Path,'w')
sw1.write("测试")

MD5加密

def GetSign(timestr):
	sginStr=""+timestr
	mD=MD5.Create();
	bytes=Encoding.UTF8.GetBytes(sginStr);
	array=mD.ComputeHash(bytes);
	sign=StringBuilder()
	for b in array:
		sign.AppendFormat("{0:X2}", b);
	return sign.ToString().lower()

DynamicObject转JSON

def	DynamicObjectToJson(obj):
	jsonSerializerProxy=JsonSerializerProxy(Encoding.GetEncoding("utf-8"),False)
	return jsonSerializerProxy.Serialize(obj)

文件压缩

def ZipFiles(zipfile,filenames):
    try:
        Path=PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, zipfile)
        sw=ZipFile.Create(Path)
        sw.BeginUpdate()
        for r in filenames:
            sw.Add(PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, r),r)
        sw.CommitUpdate()
        sw.Close()
        return Path
    except Exception as e:
        this.View.ShowMessage(str(e))

执行语句

exec('this.View.ShowMessage("测试提醒")')

HTML转换我WORD下载

 html=html.replace("<!--RichEdit.WPF1-->","")
        fileName=this.View.Model.GetValue("FBillNo")+".doc"
        Path=PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName)
        fileUrl=PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fileName)
        sw=StreamWriter(Path, False)
        sw.WriteLine(html);
        sw.Flush();
        sw.Close();
        showParameter=DynamicFormShowParameter();
        showParameter.FormId="BOS_FileDownload";
        showParameter.OpenStyle.ShowType=ShowType.Modal;
        showParameter.CustomParams.Add("url", fileUrl);
        this.View.ShowForm(showParameter);

邮件

待续

短信发送

        phs=List[PhoneInfo]();
        ph=PhoneInfo();
        ph.Phone="13800000000"
        phs.Add(ph);
        ms=CloudSMSInfo();
        ms.PhoneInfos=phs;
        ms.SMSMessage="测试短信"
        result=SMSServiceHelper.SendMessage(this.Context, ms)
        this.View.ShowMessage(result.Message)

待续

WebClient下载

webClient=WebClient();
webClient.DownloadFile(fname, filePath);
downFileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, TmNow+".zip");

            	#打开文件下载界面
showParameter = DynamicFormShowParameter();
showParameter.FormId = "BOS_FileDownLoad";
showParameter.OpenStyle.ShowType = ShowType.Modal;
showParameter.CustomComplexParams.Add("url", downFileUrl);
this.View.ShowForm(showParameter);

XML读写

待续

EXCEL读写

 ds = DBServiceHelper.ExecuteDataSet(this.Context, ksql);
        try:
            fname=DateTime.Now.ToString("yyyyMMddHHmmss")+".xls"
            filePath=PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fname);
            fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fname);
            excelHelper=ExcelOperation(this.View)
            excelHelper.BeginExport();
            #excelHelper.ExportToFile(ds.Tables[0]);
            excelHelper.DateSetToExcel(ds,True);
            
            excelHelper.EndExport(filePath, SaveFileType.XLS)
            showParameter=DynamicFormShowParameter();
            showParameter.FormId = "BOS_FileDownload";
            showParameter.OpenStyle.ShowType = ShowType.Modal;
            showParameter.CustomComplexParams.Add("url", fileUrl);
            this.View.ShowForm(showParameter);

图标赞 46
46人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!