背景
Python中,想要打開已經存在的excel的xls文件,然后在最后新的一行的數據。
折騰過程
1.找到了參考資料:
writing to existing workbook using xlwt
其實是沒有直接實現:
打開已有的excel文件,然后在文件最后寫入,添加新數據
的函數的。
只不過,可以利用:
Working with Excel Files in Python
中的庫,組合實現。
2. writing to existing workbook using xlwt
給出了示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
START_ROW
=
297
# 0 based (subtract 1 from excel row number)
col_age_november
=
1
col_summer1
=
2
col_fall1
=
3
rb
=
open_workbook(file_path,formatting_info
=
True
)
r_sheet
=
rb.sheet_by_index(
0
)
# read only copy to introspect the file
wb
=
copy(rb)
# a writable copy (I can't read values out of this, only write to it)
w_sheet
=
wb.get_sheet(
0
)
# the sheet to write to within the writable copy
for
row_index
in
range
(START_ROW, r_sheet.nrows):
age_nov
=
r_sheet.cell(row_index, col_age_november).value
if
age_nov
=
=
3
:
#If 3, then Combo I 3-4 year old for both summer1 and fall1
w_sheet.write(row_index, col_summer1,
'Combo I 3-4 year old'
)
w_sheet.write(row_index, col_fall1,
'Combo I 3-4 year old'
)
wb.save(file_path
+
'.out'
+
os.path.splitext(file_path)[
-
1
])
|
3. 剛又看到,有更簡潔的代碼:
1
2
3
4
|
from
xlutils.copy
import
copy
w
=
copy(
'book1.xls'
)
w.get_sheet(
0
).write(
0
,
0
,
"foo"
)
w.save(
'book2.xls'
)
|
4.現在打算去試試。
先去安裝xlrd:
6.再去安裝xlutils:
【記錄】Python中安裝可以讀寫excel的xls文件的xlutils模塊(需依賴於xlrd和xlwt)
7.接着可以去寫代碼了。
8.先是:
【已解決】Python中使用xlutils.copy出錯:AttributeError: ‘module’ object has no attribute ‘copy’
9.后是:
【已解決】Python中使用xlutils的copy出錯:AttributeError: ‘str’ object has no attribute ‘datemode’
10.后來是用如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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"
;
|
實現了,打開,剛剛保存的,已經存在的xls文件,
然后寫入新數據的目的。
但是有個缺點,
第一次保存時的,帶格式(標題內容為紅色粗體)的內容:
重新寫入新數據,再保存時,卻丟失了之前的格式(標題沒了紅色粗體了):
11.后來還是參考:
writing to existing workbook using xlwt
中的那個標准答案,在用xlrd.open_workbook時,添加對應的參數formatting_info=True,就可以保留原有格式了。
完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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"
;
|
1
|
|
最后重新寫入的數據,就可以保留之前的格式了(標題為紅色粗體):
總結
python中操作,本身就復雜的xls文件,還是有點小麻煩的。
想要,往已經存在的xls文件中,寫入新的行,新的數據,對應的邏輯為:
- 用xlrd.open_workbook打開已有的xsl文件
- 注意添加參數formatting_info=True,得以保存之前數據的格式
- 然后用,from xlutils.copy import copy;,之后的copy去從打開的xlrd的Book變量中,拷貝出一份,成為新的xlwt的Workbook變量
- 然后對於xlwt的Workbook變量,就是正常的:
- 通過get_sheet去獲得對應的sheet
- 拿到sheet變量后,就可以往sheet中,寫入新的數據
- 寫完新數據后,最終save保存
相關完整代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import
xlwt;
import
xlrd;
#import xlutils;
from
xlutils.copy
import
copy;
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文件的xlutils模塊(需依賴於xlrd和xlwt)