首页 > 解决方案 > Python将Excel图形导入PDF文件

问题描述

我最近尝试将我的 Excel 升级为 PDF 脚本。到目前为止,我已经成功地将 2 个单独的 PDF 文件合并为一个,这很棒。但是,我在尝试从第二个 Excel 文件中导入 Excel 图表时遇到了很多麻烦。以下是我的代码:

import pandas as pd
import pdfkit
import os, PyPDF2
from PyPDF2 import PdfFileMerger
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
options = {
    'page-size': 'A4',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'footer-right': "Page [page] of [toPage]",
    'footer-left': "This Document is CLASSIFIED"
    }

df = pd.read_excel("ReportExcel.xlsx")#input
df = df.fillna(" ")
df.to_html("ReportHTML.html", index = False)#to html
pdfkit.from_file("ReportHTML.html", "ReportPDF.pdf", configuration=config, options= options)#to pdf

df = pd.read_excel("testpie.xlsx")#input
df = df.fillna(" ")
df.to_html("testpie.html", index = False)#to html
pdfkit.from_file("testpie.html", "testpie.pdf", configuration=config, options= options)#to pdf

pdfs = ['ReportPDF.pdf', 'testpie.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(pdf)

merger.write("Final_True_Report.pdf")
merger.close()

接下来是我合并的 2 个 Excel 文件 在此处输入图像描述

第一个 Excel,ReportExcel.xlsx,基本上是报告的主体

在此处输入图像描述

第二个 excel,testpie.xlsx,包含 excel 图表

对于转换 2 个 PDF 文件并将 2 个文件合并在一起的工作,它工作得非常好。但是,我仍然坚持尝试将 Excel 图表无缝导入 PDF。有办法解决我的困境吗?非常欢迎当前使用的 pdfkit 和 pandas 的替代品!

标签: pythonexcelpandaspdfkitpypdf2

解决方案


推荐阅读