【问题标题】:How to convert an excel sheet to a pdf using python?如何使用python将excel表转换为pdf?
【发布时间】:2019-11-26 03:27:50
【问题描述】:

我有一张 Excel 表格,我想将其转换为 Python。需要为上述查询推荐包或模块。

我已尝试使用 PDFWriter。我正在使用 pandas 读取我的 excel 文件并将该数据集输入到 PDFwriter 以创建 pdf 文件。输出 pdf 似乎是空白的。

import pandas as pd
from pdfrw import PdfWriter

wb = pd.read_excel('excel file path')

wb=PdfWriter()

wb.write('result.pdf')

创建的 pdf 文件只有 1kb..

【问题讨论】:

标签: python pdf pdfrw


【解决方案1】:

这里有几点:

  1. 您正在读取第一行变量 wb 中的 DataFrame,并在下一行覆盖相同的变量。从 Excel 工作表中读取的值将会丢失。
  2. 您必须使用 addpage() 函数向您的页面添加内容。

你也可以试试这个link。

【讨论】:

    【解决方案2】:
    import pandas as pd
    import pdfkit
    df = pd.read_excel("file.xlsx")#input
    df.to_html("file.html")#to html
    pdfkit.from_file("file.html", "file.pdf")#to pdf
    

    【讨论】:

    • 请编辑您的答案以解释它如何回答问题。
    猜你喜欢
    • 2021-05-30
    • 2012-08-05
    • 2020-02-29
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2011-05-26
    • 2017-01-13
    • 2018-01-27
    相关资源
    最近更新 更多