Python-通过一些修改从xls创建pdf

问题描述 投票:2回答:1

我试图用Python编写应用程序,允许将xls文件转换为pdf。 xls文件有3列:索引,PLN价格和EUR价格(价格不变)。我想要的是生成可打印的pdf标签,其中包含每个索引的所有信息 - 大粗体索引和低于它的价格。所以基本上标签应该有大的索引,而这两个价格,换句话说,一行应该是一个精确大小的pdf页面。它还需要简单的gui 2按钮,上传文件和生成。

现在我尝试使用openpyxl来获取所有行:

import openpyxl

wb = openpyxl.load_workbook('arkusz.xlsx')
ws = wb.get_sheet_by_name('Arkusz1')
mylist = []
for row in ws.iter_rows('A{}:C{}'.format(ws.min_row,ws.max_row)):
    for cell in row:
        mylist.append(cell.value)
print (mylist)

我得到了行但现在我很难将其写入pdf。我找不到任何符合我要求的lib。你能为这个应用程序建议最好的lib吗?

python pdf openpyxl xls
1个回答
1
投票

如果您只是从excel读取然后创建原始pdf,我建议您只使用pandas.read_excel来读取.xlsx文件。

为了创建pdf部分,有几个选项,包括pydf2pdfdocumentFPDF。 FPDF库使用相当紧凑,是我在这个例子中使用的。可以找到FPDF文档here

我在下面发布了一个完全可重现的例子,使用pandas和fpdf(它还使用numpy来创建一个示例数据帧)。我在我的示例中循环遍历整个数据框,但如果您愿意,可以根据索引选择特定的行。

import pandas as pd
import numpy as np
from fpdf import FPDF

# Creating a dataframe and saving as test.xlsx in current directory
df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
writer = pd.ExcelWriter('test.xlsx')
df_1.to_excel(writer)
writer.save()

#read in the .xlsx file just created
df_2 = pd.read_excel('test.xlsx')

#creating a pdf in called test.pdf in the current directory
pdf = FPDF()
pdf.add_page()
pdf.set_xy(0, 0)
pdf.set_font('arial', 'B', 14)
pdf.cell(60)
pdf.cell(70, 10, 'Writing a PDF from python', 0, 2, 'C')
pdf.cell(-40)
pdf.cell(50, 10, 'Index Column', 1, 0, 'C')
pdf.cell(40, 10, 'Col A', 1, 0, 'C')
pdf.cell(40, 10, 'Col B', 1, 2, 'C')
pdf.cell(-90)
pdf.set_font('arial', '', 12)
for i in range(0, len(df_2)-1):
    col_ind = str(i)
    col_a = str(df_2.A.ix[i])
    col_b = str(df_2.B.ix[i])
    pdf.cell(50, 10, '%s' % (col_ind), 1, 0, 'C')
    pdf.cell(40, 10, '%s' % (col_a), 0, 0, 'C')
    pdf.cell(40, 10, '%s' % (col_b), 0, 2, 'C')
    pdf.cell(-90)
pdf.output('test.pdf', 'F')

预期的pdf输出将如下所示:pdf output

© www.soinside.com 2019 - 2024. All rights reserved.