Excelに画像ファイル貼付 ~Pythonを操るFCMPプロシジャ~
proc FCMP02/01
(水)
*---画像ファイル保存場所(パス記入);
%let _inpict=.;
*---画像ファイルを貼付したいExcelファイル保存場所(パス記入);
%let _inpath=.;
*---画像ファイルを貼付したいExcelファイル名(拡張子ごと記入);
%let _infile=Before.xlsx;
*---画像ファイル貼付後Excelファイル出力場所(パス記入);
%let _outpath=.;
*---画像ファイル貼付後Excelファイル名(拡張子ごと記入);
%let _outfile=After.xlsx;
ods listing gpath="&_inpict.";
ods graphics/reset imagename="file1" width=2.0in height=2.0in noborder;
proc sgplot data=sashelp.class;
bubble x=height y=weight size=age/group=sex dataskin=sheen;
run;
ods graphics/reset imagename="file2" width=2.0in height=2.0in noborder;
proc sgplot data=sashelp.iris noautolegend;
scatter x=species y=sepallength/markerattrs=(symbol=circlefilled) jitter group=species;
run;
filename _inpict "&_inpict.";
filename _inpath "&_inpath.";
filename _outpath "&_outpath.";
data _null_;
inpict=tranwrd(pathname("_inpict"),"\","/");
call symputx("inpict",unicodec(inpict,'utf8'));
inpath=tranwrd(pathname("_inpath"),"\","/")||"/&_infile.";
call symputx("infile",unicodec(inpath,'utf8'));
outpath=tranwrd(pathname("_outpath"),"\","/")||"/&_outfile.";
call symputx("outfile",unicodec(outpath,'utf8'));
run;
proc fcmp;
declare object py(python);
submit into py;
def PyProduct(infile,outfile):
"""Output:"""
# coding: utf-8
from openpyxl import load_workbook
import openpyxl
wb=load_workbook(infile)
wb.save(outfile)
endsubmit;
rc=py.publish();
rc=py.call('PyProduct',"&infile.","&outfile.");
run;
proc delete data=_all_; run;
proc fcmp outlib=work.fcmp.pyfuncs;
function MyFunc(arg0 $,arg1 $,arg2 $,arg3 $) $50;
length Result $50;
declare object py(python);
submit into py;
def Product(range,file,inpict,outfile):
"""Output: MyKey"""
# coding: utf-8
from openpyxl.drawing.image import Image
from openpyxl import load_workbook
import openpyxl
wb=load_workbook(outfile)
ws=wb["Figure"] #シート名
ws.add_image(Image(inpict +'/'+ file),range)
wb.save(outfile)
return file
endsubmit;
rc=py.publish();
rc=py.call('Product',arg0,arg1,arg2,arg3);
Result=py.results['MyKey'];
return(Result);
endfunc;
run;
options cmplib=work.fcmp;
%macro var(_range=,_file=);
data _null_;
x=MyFunc(&_range.,unicodec(&_file.,'utf8'),"&inpict.","&outfile.");
put x=;
run;
%mend var;
%var(_range="A1",_file="file1.png");
%var(_range="A15",_file="file2.png");
【参考】
コメント