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

我的主要思路是:

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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • python使用Image处理图片常用技巧分析

    本文实例讲述了python使用Image处理图片常用技巧.分享给大家供大家参考.具体分析如下: 使用python来处理图片是非常方便的,下面提供一小段python处理图片的代码,需要安装图像处理工具包PIL(Python Image Library). #coding=utf-8 import Image import urllib2 import StringIO import os #改变图片大小 def resize_img(img_path): try: img = Image.open

  • Python结合ImageMagick实现多张图片合并为一个pdf文件的方法

    本文实例讲述了Python结合ImageMagick实现多张图片合并为一个pdf文件的方法.分享给大家供大家参考,具体如下: 前段时间买了不少书,现在手头的书籍积累的越来越多,北京这边租住的小屋子空间越来越满了.自从习惯了笔记本触摸板的手势操作之后,我偶觉得使用电脑看电子文档也挺享受的.于是想把自己的部分书籍使用手机拍照,然后合并成一个pdf文件. 最初尝试过找成熟的Windows软件,但是始终没有找到一个好用的软件.想写脚本处理,一直也没有实现.偶然查看ImageMagick软件的说明,找到了

  • 详解python读取image

    python 读取image 在python中我们有两个库可以处理图像文件,scipy和matplotlib. 安装库 pip install matplotlib pillow scipy 用法 from scipy.misc import imread data = imread(image_root) #data是 ndarray对象 import matplotlib.image as mpimg data = mpimg.imread(image_root) #data是 ndarra

  • Python图像的增强处理操作示例【基于ImageEnhance类】

    本文实例讲述了Python图像的增强处理操作.分享给大家供大家参考,具体如下: python中PIL模块中有一个叫做ImageEnhance的类,该类专门用于图像的增强处理,不仅可以增强(或减弱)图像的亮度.对比度.色度,还可以用于增强图像的锐度. 具体见下面的例子: #-*- coding: UTF-8 -*- from PIL import Image from PIL import ImageEnhance #原始图像 image = Image.open('lena.jpg') imag

  • python提示No module named images的解决方法

    本文讲述了python提示No module named images的解决方法,非常实用!分享给大家供大家参考.具体方法如下: 出现提示:ImportError: No module named images 表示找不到images模块 可将: import images 替换为: import wx.py.images as images 将: images.getNewBitmap 替换为: images.getPyBitmap() 即可解决问题. 希望本文所述对大家的Python程序设计

  • Python读取视频的两种方法(imageio和cv2)

    用python读取视频有两种主要方法,大家可依据自己的需求进行使用. 方法一: 使用imageio库,没有安装的可用pip安装或自己下载,安装好后重启终端即可调用. import pylab import imageio #视频的绝对路径 filename = '/path/to/your/video.mp4' #可以选择解码工具 vid = imageio.get_reader(filename, 'ffmpeg') for im in enumerate(vid): #image的类型是ma

  • python中ImageTk.PhotoImage()不显示图片却不报错问题解决

    发现问题 今天在使用ImageTk.photoImage()显示图片时,当把包含该函数放在自定义函数里时,不能正常显示,移到函数为又可正常显示,所以想到可能是变量不是全局性的缘故,改为全局变量后果然可正常显示,下面贴出前后代码对比 示例代码 ImageTk.photoImage()在自定义函数外使用(正常显示): ImageTk.photoImage()在自定义函数内使用(无法显示): ImageTk.photoImage()在自定义函数内使用(正常显示): 完整代码: 总结 以上就是这篇文章的

  • python skimage 连通性区域检测方法

    涉及到的函数为 import matplotlib.pyplot as plt from skimage import measure, color labels = measure.label(img4[:,:,0], connectivity=2) dst = color.label2rgb(labels) plt.imshow(dst) labels为整个灰度图像的坐标的类别标签,值为[0, max_label], 一个连通区域为一个 lable . 以上这篇python skimage

  • Python3实现转换Image图片格式

    前言 首先图片格式转换的方法有很多,但是转二进制字节流的,我搜了一下午终于在 stackoverflow上搜到了 说一下为什么要在线转这个图片格式 额,一名Python3 spider, 需求要爬去文章,并且把里面的图片,上传到本公司的七牛云上面, 意思就是,图片不需要下载到本地,爬取到图片url后,读取图片二进制数据,然后上传到自己公司七牛云上面,并且替换其格式(这一点很坑) 上传图片二进制数据到七牛云上面不难, 照着七牛云API文档基本就能行,有一点很坑,七牛云官方文档Python API

  • 使用ImageMagick进行图片缩放、合成与裁剪(js+python)

    最近的项目里面需要对书籍的封面进行处理,就是加一条阴影线形成书脊的凹凸感,然后将书脊切出,分成两部分,以便客户端实现打开动画.由于需要在服务器端处理,使用就研究使用imagemagick来进行.同时准备封装了一个Node.js和Python的方法,主要还是讲一下然后使用imagemagick来对图片进行缩放.合成后进行裁剪吧. 首先素材文件如下(左边未处理封面,右边为需要合成上去的阴影): 安装ImageMagick的过程就不讲了,可以参考官网的安装方法:http://www.imagemagi

随机推荐