从 PrettyTable 到 pdf 的表格
Posted
技术标签:
【中文标题】从 PrettyTable 到 pdf 的表格【英文标题】:Table from PrettyTable to pdf 【发布时间】:2019-09-24 21:08:29 【问题描述】:我使用 PrettyTable 创建了一个表。我想将输出保存为 .pdf 文件,但我唯一能做的就是将其保存为 .txt。
如何将其保存为 .pdf 文件? 我安装了 FPDF 库,但我被这个卡住了。
# my table is saved as 'data' variable name
# I saved the table ('data') as .txt file
data = x.get_string()
with open('nameoffile.txt', 'w') as f:
f.write(data)
print(data)
【问题讨论】:
【参考方案1】:PrettyTable 不用于将数据导出为 pdf 文件。用于显示ASCII表。
以下代码是自制方法,可以解答您的问题。
假设您有这个要导出的漂亮表格:
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
首先,您需要获取表格的内容。该模块不应该以这种方式工作:它假定您有一个要显示的表格内容。让我们反其道而行之:
def get_data_from_prettytable(data):
"""
Get a list of list from pretty_data table
Arguments:
:param data: data table to process
:type data: PrettyTable
"""
def remove_space(liste):
"""
Remove space for each word in a list
Arguments:
:param liste: list of strings
"""
list_without_space = []
for mot in liste: # For each word in list
word_without_space = mot.replace(' ', '') # word without space
list_without_space.append(word_without_space) # list of word without space
return list_without_space
# Get each row of the table
string_x = str(x).split('\n') # Get a list of row
header = string_x[1].split('|')[1: -1] # Columns names
rows = string_x[3:len(string_x) - 1] # List of rows
list_word_per_row = []
for row in rows: # For each word in a row
row_resize = row.split('|')[1:-1] # Remove first and last arguments
list_word_per_row.append(remove_space(row_resize)) # Remove spaces
return header, list_word_per_row
然后您可以将其导出为 pdf 文件。这是一种解决方案:
from fpdf import FPDF
def export_to_pdf(header, data):
"""
Create a a table in PDF file from a list of row
:param header: columns name
:param data: List of row (a row = a list of cells)
:param spacing=1:
"""
pdf = FPDF() # New pdf object
pdf.set_font("Arial", size=12) # Font style
epw = pdf.w - 2*pdf.l_margin # Witdh of document
col_width = pdf.w / 4.5 # Column width in table
row_height = pdf.font_size * 1.5 # Row height in table
spacing = 1.3 # Space in each cell
pdf.add_page() # add new page
pdf.cell(epw, 0.0, 'My title', align='C') # create title cell
pdf.ln(row_height*spacing) # Define title line style
# Add header
for item in header: # for each column
pdf.cell(col_width, row_height*spacing, # Add a new cell
txt=item, border=1)
pdf.ln(row_height*spacing) # New line after header
for row in data: # For each row of the table
for item in row: # For each cell in row
pdf.cell(col_width, row_height*spacing, # Add cell
txt=item, border=1)
pdf.ln(row_height*spacing) # Add line at the end of row
pdf.output('simple_demo.pdf') # Create pdf file
pdf.close() # Close file
最后,你只需要调用这两个方法:
header, data = get_data_from_prettytable(x)
export_to_pdf(header, data)
【讨论】:
以上是关于从 PrettyTable 到 pdf 的表格的主要内容,如果未能解决你的问题,请参考以下文章
Python PrettyTable:在表格标题上方添加标题