Python EXCEL转图片转PDF, HTML转图片转PDF, EXCEL 转 HTML, EXCEL 转 PDF, EXCEL 转 image, HTML 转PDF, HTML 转 image,

# -*- 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/")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡泡码客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值