我想用txt制作一些excel文件。 我刚刚开始python学习。
下面的是我的文本文件。
Aggregate,Total,Used,Avail
Node1_aggr0/.snapshot,0,0,0
Node1_aggr1/.snapshot,0,0,0
Node1_aggr2,19194,16147,3047
Node2_aggr1/.snapshot,0,0,0
Node1_aggr0,492,469,22
Node1_aggr1,73333,65602,7731
Node2_aggr1,73333,66823,6510
Node2_aggr0,492,469,22
Node2_aggr2,19194,16834,2359
Node1_aggr2/.snapshot,0,0,0
Node2_aggr0/.snapshot,0,0,0
Node2_aggr2/.snapshot,0,0,0
下面是我的脚本。
import pandas as pd
import xlsxwriter
aggr_file = 'sum_aggr_size.txt'
aggr_table = pd.read_table(aggr_file, encoding='utf_8', sep=',', header=None, names=['Aggregate', 'Total', 'Used', 'Avail'], lineterminator='\n')
aggr_count_cell = sum(1 for line in open('sum_aggr_size.txt'))
workbook = xlsxwriter.Workbook('storage_info.xlsx', {'strings_to_numbers': True})
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True, 'border': 1})
num_border = workbook.add_format({'border': 1, 'num_format': '@'})
merge_format = workbook.add_format({'bold': True, 'border': 1,'align': 'center', 'valign': 'vcenter'})
worksheet.merge_range('A1:D1', 'storage information', merge_format)
worksheet.write('A2', 'Aggr', bold)
worksheet.write('B2', 'Total', bold)
worksheet.write('C2', 'Used', bold)
worksheet.write('D2', 'Avail', bold)
data = (aggr_table)
row = 2
col = 0
sum_total = "B" + str(aggr_count_cell)
sum_used = "C" + str(aggr_count_cell)
sum_avail = "D" + str(aggr_count_cell)
for index, temp_data in data.iterrows():
if index != 0:
worksheet.write(row, col, temp_data[0], num_border)
worksheet.write(row, col + 1, temp_data[1], num_border)
worksheet.write(row, col + 2, temp_data[2], num_border)
worksheet.write(row, col + 3, temp_data[3], num_border)
row += 1
worksheet.write(row, 0, 'Total', bold)
worksheet.write(row, 1, '=SUM(B2:' + sum_total + ')', num_border)
worksheet.write(row, 2, '=SUM(C2:' + sum_used + ')', num_border)
worksheet.write(row, 3, '=SUM(D2:' + sum_avail + ')', num_border)
workbook.close()
我想得到如下图所示的结果。
我想绘制单元格边框。 我的剧本现在不起作用了。
请帮帮我。
答案 0 :(得分:1)
您可以参考代码。
import pandas as pd
import xlsxwriter
aggr_file = 'sum_aggr_size.txt' # your text file
aggr_table = pd.read_table(aggr_file, encoding='utf_8', sep=',', header=None,
names=['aggr', 'total', 'used', 'avail'], lineterminator='\n')
workbook = xlsxwriter.Workbook('aggr.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
border = workbook.add_format({'border': 1})
worksheet.write('A1', 'Aggr', bold)
worksheet.write('B1', 'Total', bold)
worksheet.write('C1', 'Used', bold)
worksheet.write('D1', 'Avail', bold)
data = (aggr_table)
row = 1
col = 0
for index, temp_date in data.iterrows():
if index != 0:
worksheet.write(row, col, temp_date[0], border)
worksheet.write(row, col + 1, temp_date[1].replace("GB", ""), border)
worksheet.write(row, col + 2, temp_date[2].replace("GB", ""), border)
worksheet.write(row, col + 3, temp_date[3].replace("GB", ""), border)
row += 1
worksheet.write(row, 0, 'Total', border)
worksheet.write(row, 1, '=SUM(B2:B8)', border)
worksheet.write(row, 2, '=SUM(C2:C8)', border)
worksheet.write(row, 3, '=SUM(D2:D8)', border)
workbook.close()