当前位置:   article > 正文

Python EXCEL转图片转PDF, HTML转图片转PDF, EXCEL 转 HTML, EXCEL 转 PDF, EXCEL 转 image, HTML 转PDF, HTML 转 image,_pandas excel转化pdf

pandas excel转化pdf
# -*- 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/")


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/249998
推荐阅读
相关标签
  

闽ICP备14008679号