Python将Excel图形导入PDF文件

2024-06-11 16:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近尝试将Excel升级为PDF脚本。到目前为止,我已经成功地将两个单独的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文件 enter image description here

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

enter image description here

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

对于转换2个PDF文件并将2个文件合并在一起的工作,它工作得非常好。然而,我仍然在尝试无缝地将Excel图形导入PDF。有办法解决我的困境吗?目前正在使用的pdfkit和熊猫的替代品非常受欢迎


Tags: 文件toinmarginimportdfpdfhtml