Python实现将Excel转换成为image的方法

yipeiwu_com5年前Python基础

我的主要思路是:

Excel -> Html -> Image

代码如下:

# -*- coding:utf-8 -*-
__author__ = 'YangXin'
import sys
import pandas as pd
import codecs
import imgkit
reload(sys)
sys.setdefaultencoding('utf-8')
 
 
# ReportImage -> report convert include multiple sheets into pictures
class ReportImage:
 
 def __init__(self):
  pass
 
 # excel_html -> convert excel include multiple sheets into multiple html file
 # excel_file -> file
 # html_path -> path
 @staticmethod
 def excel_html(excel_file, html_path):
  html_list = []
  excel_obj = pd.ExcelFile(excel_file)
  sheet_list = excel_obj.sheet_names
  index = 0
  for i in sheet_list:
   html_file = html_path + i + ".html"
   excel_data = excel_obj.parse(excel_obj.sheet_names[index])
   with codecs.open(html_file, 'w', 'utf-8') as html:
    html.write(excel_data.to_html(header=True, index=True))
   html_list.append(html_file)
   index += 1
  return html_list
 
 # html_image -> convert htmls into pictures file
 # html_list -> list
 # image_path -> path
 @staticmethod
 def html_image(html_list, image_path):
  index = 0
  for i in html_list:
   img_obj = image_path + str(index) + ".png"
   with open(i, 'r') as html_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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python编程之基于概率论的分类方法:朴素贝叶斯

Python编程之基于概率论的分类方法:朴素贝叶斯

概率论啊概率论,差不多忘完了。 基于概率论的分类方法:朴素贝叶斯 1. 概述 贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯定理为基础,故统称为贝叶斯分类。本章首先介绍贝叶斯分类算法...

python中将阿拉伯数字转换成中文的实现代码

复制代码 代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exce...

tensorflow实现简单的卷积网络

使用tensorflow实现一个简单的卷积神经,使用的数据集是MNIST,本节将使用两个卷积层加一个全连接层,构建一个简单有代表性的卷积网络。 代码是按照书上的敲的,第一步就是导入数据库...

跟老齐学Python之不要红头文件(1)

跟老齐学Python之不要红头文件(1)

这两天身体不给力,拖欠了每天发讲座的约定,看官见谅。 红头文件,是某国特别色的东西,在python里不需要,python里要处理的是计算机中的文件,包括文本的、图片的、音频的、视频的等等...

学习python类方法与对象方法

学习python类方法与对象方法

本文实例针对python的类方法与对象方法进行学习研究,具体内容如下 class Test_Demo: TEST = 'test_value' def __init__(s...