Python实现将Excel转换成为image的方法
我的主要思路是:
Excel->Html->Image
代码如下:
#-*-coding:utf-8-*-
__author__='YangXin'
importsys
importpandasaspd
importcodecs
importimgkit
reload(sys)
sys.setdefaultencoding('utf-8')
#ReportImage->reportconvertincludemultiplesheetsintopictures
classReportImage:
def__init__(self):
pass
#excel_html->convertexcelincludemultiplesheetsintomultiplehtmlfile
#excel_file->file
#html_path->path
@staticmethod
defexcel_html(excel_file,html_path):
html_list=[]
excel_obj=pd.ExcelFile(excel_file)
sheet_list=excel_obj.sheet_names
index=0
foriinsheet_list:
html_file=html_path+i+".html"
excel_data=excel_obj.parse(excel_obj.sheet_names[index])
withcodecs.open(html_file,'w','utf-8')ashtml:
html.write(excel_data.to_html(header=True,index=True))
html_list.append(html_file)
index+=1
returnhtml_list
#html_image->converthtmlsintopicturesfile
#html_list->list
#image_path->path
@staticmethod
defhtml_image(html_list,image_path):
index=0
foriinhtml_list:
img_obj=image_path+str(index)+".png"
withopen(i,'r')ashtml_file:
imgkit.from_file(html_file,img_obj,options={"encoding":"UTF-8"})
index+=1
if__name__=='__main__':
html_list=ReportImage.excel_html("/xxx.xlsx","/yyy/")
ReportImage.html_image(html_list,"/zzz/")
以上这篇Python实现将Excel转换成为image的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。