问题描述
我正在尝试使用Python编写应用程序,它将允许将xls文件转换为pdf. xls文件包含3列:索引,PLN价格和EUR价格(价格不变).我想要的是为每个索引生成包含所有此信息的可打印pdf标签-大胆的索引并在其价格以下.因此,基本上标签应该具有较大的索引,并且这两个价格都应该正确,换句话说,一行应该是一个pdf页面.而且它还需要有简单的向导-2个按钮,即可上传文件并生成文件.
I am trying to write app in Python which will allow to convert xls file to pdf. The xls file has 3 columns: Index, PLN price and EUR price(prices are constant). What I want, is to generate printable pdf label with all this info for each index- big bolded index and below it prices. So basically the label should have big index, and these two prices, in other words one row should be one pdf page in exact size. And it also needs to have simple gui- just 2 buttons, upload file and generate.
现在我尝试使用openpyxl来获取所有行:
For now I tried with openpyxl to get all the rows:
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吗?
I get the rows but now I have trouble to write it to pdf. I can't find any lib that will suit my requirements. Could you please advise best lib for this app?
推荐答案
如果您只是从excel中读取内容,然后创建原始pdf文件,我建议您仅使用pandas.read_excel
来读取.xlsx文件.
If you're just reading from excel and then creating an original pdf, I would recommend just using pandas.read_excel
for reading the .xlsx file.
要创建pdf部件,有几个选项,包括pydf2
,pdfdocument
和FPDF
. FPDF库相当易于使用,这就是我在此示例中使用的库.可以在此处找到FPDF文档.
For creating the pdf part, there are several options including pydf2
, pdfdocument
and FPDF
. The FPDF library is fairly stragihtforward to use and is what I've used in this example. FPDF Documentation can be found here.
我在下面发布了一个完全可复制的示例,使用了pandas和fpdf(它也使用numpy创建了示例数据框).我在示例中遍历了整个数据框,但如果愿意,您可以根据索引选择特定的行.
I've posted a fully reproducible example below, using pandas and fpdf (it also uses numpy to create a sample dataframe). I loop through the whole dataframe in my example but you could select specific rows based on the index if you wanted to do so.
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输出如下所示:
Expected pdf output would look like this:
这篇关于Python-从xls创建pdf并进行一些修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!