//写入方法一:
import
xlwt;
import
xlrd;
#import xlutils;
from
xlutils.copy
import
copy;
#init xls file
#styleBlueBkg= xlwt.easyxf('pattern: pattern solid, fore_colour sky_blue;');
#styleBold = xlwt.easyxf('font: bold on');
styleBoldRed
=
xlwt.easyxf(
'font: color-index red, bold on'
);
headerStyle
=
styleBoldRed;
wb
=
xlwt.Workbook();
ws
=
wb.add_sheet(gConst[
'xls'
][
'sheetName'
]);
ws.write(
0
,
0
,
"Header"
, headerStyle);
ws.write(
0
,
1
,
"CatalogNumber"
, headerStyle);
ws.write(
0
,
2
,
"PartNumber"
, headerStyle);
wb.save(gConst[
'xls'
][
'fileName'
]);
#open existed xls file
#newWb = xlutils.copy(gConst['xls']['fileName']);
#newWb = copy(gConst['xls']['fileName']);
oldWb
=
xlrd.open_workbook(gConst[
'xls'
][
'fileName'
]);
print
oldWb;
#<xlrd.book.Book object at 0x000000000315C940>
newWb
=
copy(oldWb);
print
newWb;
#<xlwt.Workbook.Workbook object at 0x000000000315F470>
newWs
=
newWb.get_sheet(
0
);
newWs.write(
1
,
0
,
"value1"
);
newWs.write(
1
,
1
,
"value2"
);
newWs.write(
1
,
2
,
"value3"
);
print
"write new values ok"
;
newWb.save(gConst[
'xls'
][
'fileName'
]);
print
"save with same name ok"
;
==================================================
//写入方法二:
import
xlwt;
import
xlrd;
#import xlutils;
from
xlutils.copy
import
copy;
#init xls file
#styleBlueBkg= xlwt.easyxf('pattern: pattern solid, fore_colour sky_blue;');
#styleBold = xlwt.easyxf('font: bold on');
styleBoldRed
=
xlwt.easyxf(
'font: color-index red, bold on'
);
headerStyle
=
styleBoldRed;
wb
=
xlwt.Workbook();
ws
=
wb.add_sheet(gConst[
'xls'
][
'sheetName'
]);
ws.write(
0
,
0
,
"Header"
, headerStyle);
ws.write(
0
,
1
,
"CatalogNumber"
, headerStyle);
ws.write(
0
,
2
,
"PartNumber"
, headerStyle);
wb.save(gConst[
'xls'
][
'fileName'
]);
#open existed xls file
#newWb = xlutils.copy(gConst['xls']['fileName']);
#newWb = copy(gConst['xls']['fileName']);
oldWb
=
xlrd.open_workbook(gConst[
'xls'
][
'fileName'
], formatting_info
=
True
);
print
oldWb;
#<xlrd.book.Book object at 0x000000000315C940>
newWb
=
copy(oldWb);
print
newWb;
#<xlwt.Workbook.Workbook object at 0x000000000315F470>
newWs
=
newWb.get_sheet(
0
);
newWs.write(
1
,
0
,
"value1"
);
newWs.write(
1
,
1
,
"value2"
);
newWs.write(
1
,
2
,
"value3"
);
print
"write new values ok"
;
newWb.save(gConst[
'xls'
][
'fileName'
]);
print
"save with same name ok"
;
【整理】Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据
转载上一篇:SWF to EXE 工具制作
下一篇:ubuntu操作命令
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python计算多个Excel文件数据的平均值
本文介绍基于Python语言,对大量不同的Excel文件加以跨文件、逐单元格平均值计算的方法~
Python Excel 平均值 跨文件平均值 平均数 -
Python读取写入数据到Excel文件
Python读取写入数据到Excel文件
python excel pandas csv execl -
springcloud查看接口列表
环境:Spring Cloud 2021.0.7 + Spring Boot 2.7.12配置依赖maven依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId&
springcloud查看接口列表 spring cloud 负载均衡 spring 客户端