# -*- coding:utf-8 -*-
__author__ = 'Mooney'
import codecs
import imgkit
import pandas as pd
import pdfkit as pdfkit
class ExcelToPdf:
def __init__(self):
super(ExcelToPdf, self).__init__()
self.html_head = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>"""
self.html_end = """</body>
</html>"""
def excel_html(self, excel_path, html_path):
"""
excel to html
:param excel_path: excel 路径
:param html_path: html 存放 路径
:return: html 路径集合
"""
html_paths = []
excel_obj = pd.ExcelFile(excel_path) # excel 文件对象
excel_sheets = excel_obj.sheet_names # 获取 excel 所有单元
# 将每个单元转换为 html 文件
for index, sheet in enumerate(excel_sheets):
html_path = html_path + sheet + ".html"
# 获取本单元 excel 信息
excel_data = excel_obj.parse(excel_obj.sheet_names[index])
with codecs.open(html_path, 'w', 'utf-8') as html:
# 加上头尾部, 防止中文乱码
html_data = self.html_head + excel_data.to_html(header=True, index=True) + self.html_end
html.write(html_data)
html_paths.append(html_path)
return html_paths
@staticmethod
def html_pdf(html_paths, pdf_path):
"""
html to pdf
:param html_paths: html 路径
:param pdf_path: pdf 存放 结果 路径
:return:
"""
for index, html_path in enumerate(html_paths):
pdf_obj = pdf_path + str(index) + ".pdf"
with open(html_path, "r", encoding="utf-8") as html_file:
pdfkit.from_file(html_file, pdf_obj)
@staticmethod
def html_image(html_paths, image_path):
"""
html to image
:param html_paths: html 路径
:param image_path: image 存放 结果 路径
:return:
"""
for index, html_path in enumerate(html_paths):
img_obj = image_path + str(index) + ".png"
with open(html_path, "r", encoding="utf-8") as html_file:
imgkit.from_file(html_file, img_obj)
if __name__ == '__main__':
tool = ExcelToPdf()
# excel 转 html
html_paths = tool.excel_html("./excel/泰国.xlsx", "./html/")
# html 转 pdf
tool.html_pdf(html_paths, "./pdf/")
# html 转 image
tool.html_image(html_paths, "./image/")
Python EXCEL转图片转PDF, HTML转图片转PDF, EXCEL 转 HTML, EXCEL 转 PDF, EXCEL 转 image, HTML 转PDF, HTML 转 image,
最新推荐文章于 2024-08-27 19:00:00 发布