首页 > 编程语言 >利用Python从现有的Excel表格中复制指定列生成新的Excel文件

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

时间:2023-09-22 14:13:05浏览次数:43  
标签:table2 style curr 表格 Python Excel write zx row

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)

标签:table2,style,curr,表格,Python,Excel,write,zx,row
From: https://www.cnblogs.com/flash99/p/17722181.html

相关文章

  • 【Python爬虫】批量爬取豆瓣电影排行Top250
    ​    今天给大家分享下我刚开始接触Python时学习的爬虫程序,代码部分很简单,不过当时刚开始学习时还是走了不少弯路的。这个爬虫程序应该是很多书里面的入门练手程序,主要就是去豆瓣爬取电影评分排行前250。        本篇文章只做学习交流使用,不涉及任何商业用途......
  • python中,如何优雅的解析和管理命令行参数
    背景我们在编写python程序时,程序中经常会提供多种功能或者模式,在实际使用时根据不同的参数使用不同的功能。那么如何获取命令行传入进来的参数呢?一般方法一般情况下,我们会使用sys模块,如......
  • [885] How to generate automated tables in Word document with Python
    ref:HowtoGenerateAutomatedWordDocumentswithPythonref:docxtpl快速上手使用,数据填入以及循环写入表格CreatingaTemplateBeforeyoucanproceed,youmustfirstcreateyourveryowntemplatedocumentthatisbasicallyanormalMicrosoftWordDocument......
  • [884] How to generate automated Word documents by Python
    ref:python-docxref:HowtoGenerateAutomatedWordDocumentswithPythonref:AutomatingWordDocumentsfromExcelUsingPython|‘docxtpl’Tutorialref:docxtpl快速上手使用,数据填入以及循环写入表格ref:探究Python中的文档自动化工具——docxtplref:Python......
  • 表格动态合并,序号连续
    效果:<el-table:data="tableData"style="width:100%"v-loading="tableDataLoading":header-cell-style="{background:'#FAFAFA'}":span-method="objectSpanMethod"......
  • Excel根据身份证提取生日以及年龄
    在编辑栏,输入函数公式:=TEXT(MID(B2,7,8),"00-00-00"),然后按【Ctrl+Enter】结束确认,即可批量提取出:员工身份证中的出生日期;(公式中的B2代表着身份证号所在列)Datedif函数公式:=DATEDIF(TEXT(MID(B2,7,8),"00-00-00"),TODAY(),"y");即可计算出:身份证号中的年龄! 原文地址:https://bai......
  • 在线问诊 Python、FastAPI、Neo4j — 创建 节点关系
    目录关系:症状-检查关系:疾病-症状代码重构relationship_data.csv症状,检查,疾病,药品,宜吃,忌吃"上下楼梯疼,不能久站,感觉有点肿","膝关节核磁","右膝髌上囊及关节腔少量积液","扶他林","西红柿,香蕉","辣椒,大蒜""眼睛胀痛,干涩,畏光,眼胀,眼痛,看东西有时候清楚有时候不清楚......
  • 【Python深度学习】深度学习中框架和模型的区别
        深度学习是人工智能领域的一股强大力量,它的快速发展离不开深度学习框架和模型的进步。本文将介绍深度学习框架和模型的基本概念、它们之间的联系与区别,以及如何根据项目需求选择合适的框架和模型。一、深度学习框架        深度学习框架是进行深度学习研......
  • 已解决The following specifications were found to be incompatible with the existi
    已解决Thefollowingspecificationswerefoundtobeincompatiblewiththeexistingpythoninstallation文章目录报错问题解决方法PS报错问题之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。问题描述如下:UnsatisfiableErr......
  • 已解决tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index
    已解决tensorflow.python.framework.errors_impl.InvalidArgumentError:sliceindex1ofdimension0outofbounds.文章目录报错问题解决方法声明报错问题之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。问题描述如下:tensor......