利用Python从现有的Excel表格中复制指定列生成新的Excel文件

import xlrd
import xlwt
import patterns as patterns

file1 = '通讯专线汇总统计表.xls'
file2 = '附件:历史存量邮电费明细.xls'

data1 = xlrd.open_workbook(file1)

table1 = data1.sheet_by_index(0)

nrows1 = table1.nrows

zx_infos = []


#读取“通讯专线汇总统计表.xls”文件中信息
for curr_row in range (1,nrows1):
  zx_number = table1.cell_value(curr_row,1) #读取专线号内容
  zx_name = table1.cell_value(curr_row,2) #读取专线名称内容
  zx_distance = table1.cell_value(curr_row,3) #读取距离内容
  zx_type = table1.cell_value(curr_row,4) #读取线路类型内容
  zx_bandwith = table1.cell_value(curr_row,5) #读取带宽内容
  zx_provider = table1.cell_value(curr_row,6) #读取运营商内容
  zx_Aaddress = table1.cell_value(curr_row,7) #读取A端地址内容
  zx_Zaddress = table1.cell_value(curr_row,8) #读取Z端地址内容
  zx_price = table1.cell_value(curr_row,12) #读取月租内容
  zx_dep = table1.cell_value(curr_row,13) #读取使用部门内容
  zx_info = {
    '运营商':zx_provider,
    '专线号':zx_number,
    '专线名称':zx_name,
    '距离':zx_distance,
    '类型':zx_type,
    '带宽':zx_bandwith,
    '本端':zx_Aaddress,
    '对端':zx_Zaddress,
    '月租(含税:元)':zx_price,
    '年租金(含税:元)':zx_price*12,
    '使用部门':zx_dep,
  }
  zx_infos.append(zx_info)


workbook = xlwt.Workbook(encoding="utf-8")

table2 = workbook.add_sheet('01 存量线路')
title_style = xlwt.XFStyle() #初始化标题格式
style = xlwt.XFStyle() #初始化正文格式

title_font = xlwt.Font() #初始化标题字体
title_font.name = '华文细黑' #设置标题字体为华为细黑
title_font.bold = True #设置标题字体加粗
title_font.height = 20*10


font = xlwt.Font() #初始化标题字体
font.name = '华文细黑' #设置标题字体为华为细黑
font.bold = False #设置标题字体加粗
font.height = 20*10


title_pattern = xlwt.Pattern()
title_pattern.pattern = xlwt.Pattern.SOLID_PATTERN
title_pattern.pattern_fore_colour = 41

alignment = xlwt.Alignment() #初始化对齐方式
alignment.horz = 0x02 #水平居中
alignment.vert = 0x02 #垂直居中

borders = xlwt.Borders() #初始化边框
borders.left = 1 #左边框细实线
borders.right =1 #右边框细实线
borders.top = 1 #上边框细实线
borders.bottom = 1 #底边框细实线

title_style.font = title_font
title_style.alignment = alignment
title_style.borders = borders
title_style.pattern = title_pattern

style.font = font
style.alignment = alignment
style.borders = borders


table2.write(0,0,'运营商',title_style)
table2.write(0,1,'专线号',title_style)
table2.write(0,2,'专线名称',title_style)
table2.write(0,3,'距离',title_style)
table2.write(0,4,'类型',title_style)
table2.write(0,5,'带宽',title_style)
table2.write(0,6,'本端',title_style)
table2.write(0,7,'对端',title_style)
table2.write(0,8,'月租(含税:元)',title_style)
table2.write(0,9,'年租金(含税:元)',title_style)
table2.write(0,10,'使用部门',title_style)


curr_row = 1
for zx in zx_infos:
  table2.write(curr_row,0,zx['运营商'],style)
  table2.write(curr_row,1,str(zx['专线号']),style)
  table2.write(curr_row,2,zx['专线名称'],style)
  table2.write(curr_row,3,zx['距离'],style)
  table2.write(curr_row,4,zx['类型'],style)
  table2.write(curr_row,5,zx['带宽'],style)
  table2.write(curr_row,6,zx['本端'],style)
  table2.write(curr_row,7,zx['对端'],style)
  table2.write(curr_row,8,zx['月租(含税:元)'],style)
  table2.write(curr_row,9,zx['年租金(含税:元)'],style)
  table2.write(curr_row,10,zx['使用部门'],style)
curr_row +=1

workbook.save(file2)

posted @ 2023-09-22 13:58  Flash99  阅读(396)  评论(0)    收藏  举报