乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Python</em> 操作<em>Excel</em>,怎么&

<em>Python</em> 操作<em>Excel</em>,怎么&

作者:乔山办公网日期:

返回目录:excel表格制作


Python中一般使用xlrd(excel read)来读取Excel文件e69da5e887aa7a686964616f363,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取excel是不能对其进行操作的:xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作。而xlwt.Workbook()返回的xlwt.Workbook类型的save(filepath)方法可以保存excel文件。

因此对于读取和生成Excel文件都非常容易处理,但是对于已经存在的Excel文件进行修改就比较麻烦了。不过,还有一个xlutils(依赖于xlrd和xlwt)提供复制excel文件内容和修改文件的功能。其实际也只是在xlrd.Book和xlwt.Workbook之间建立了一个管道而已。

xlutils.copy模块的copy()方法实现了这个功能,示例代码如下:

from xlrd import open_workbookfrom xlutils.copy import copy

rb = open_workbook('m:\\1.xls')
#通过sheet_by_index()获取的sheet没有write()方法rs = rb.sheet_by_index(0)

wb = copy(rb)
#通过get_sheet()获取的sheet有write()方法ws = wb.get_sheet(0)
ws.write(0, 0, 'changed!')

wb.save('m:\\1.xls')

练习代码(通过xlrd 读取 & 写入,再借用copy进行保存):

特别注意:由于copy保存实质上是通过xlwt进行保存的,而实际上xlwt保存的文件。

而通过xlwt只能写入xls文件,不能写入xlsx文件。

import xlrdfrom xlwt import *from xlutils.copy import copy

xlsfile = 'test.xls'book = xlrd.open_workbook(xlsfile)

sheet_name = book.sheet_names()print(sheet_name)

sheet = book.sheet_by_index(1)
nrows = sheet.nrows
ncols = sheet.ncolsprint(nrows)print(ncols)

row_data = sheet.row_values(0)
col_data = sheet.col_values(0)print(row_data)print(col_data)

cell_value = sheet.cell_value(3,0)print(cell_value)
cell_value2 = sheet.cell(3,0)print(cell_value2)

sheet.put_cell(1,2,1,"test",0)
cell_value2 = sheet.cell(1,1)print(cell_value2)#保存xlsfilewb = copy(book)
wb.save(xlsfile)



就是把插e79fa5e98193e78988e69d83330入行之后值重新输出来。

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;
from xlutils.copy import copy;

#styleBoldRed = xlwt.easyxf('font: color-index red, bold on');
#headerStyle = styleBoldRed;
#wb = xlwt.Workbook();
#ws = wb.add_sheet('sheetName');
#ws.write(0, 0, "Col1", headerStyle);
#ws.write(0, 1, "Col2", headerStyle);
#ws.write(0, 2, "Col3", headerStyle);
#wb.save('fileName.xls');

#open existed xls file
oldWb = xlrd.open_workbook("fileName.xls", formatting_info=True);
oldWbS = oldWb.sheet_by_index(0)
newWb = copy(oldWb);
newWs = newWb.get_sheet(0);
inserRowNo = 1
newWs.write(inserRowNo, 0, "value1");
newWs.write(inserRowNo, 1, "value2");
newWs.write(inserRowNo, 2, "value3");

for rowIndex in range(inserRowNo, oldWbS.nrows):
for colIndex in range(oldWbS.ncols):
newWs.write(rowIndex + 1, colIndex, oldWbS.cell(rowIndex, colIndex).value);
newWb.save('fileName.xls');
print "save with same name ok";

用Gestalt能够改变现状的Javascript库与Silverlight结合后,它可以让开发者直接在HTML中嵌入Python,在查看站点源代码时,会看到类似这样的代码

<script language="python">

将这句话包含在页面顶部,这就是在着手写Python前所要做的所有准备。它会将内联的Python代码传给Silverlight运行时,该运行时支持Dynamic Language Runtime。



确实还真就没用过这样的函数,不过可以自己636f70797a64366实现。

就是把插入行之后值重新输出来。

import xlwt;
import xlrd;
from xlutils.copy import copy;
 
#styleBoldRed   = xlwt.easyxf('font: color-index red, bold on');
#headerStyle = styleBoldRed;
#wb = xlwt.Workbook();
#ws = wb.add_sheet('sheetName');
#ws.write(0, 0, "Col1",        headerStyle);
#ws.write(0, 1, "Col2", headerStyle);
#ws.write(0, 2, "Col3",    headerStyle);
#wb.save('fileName.xls');

#open existed xls file
oldWb = xlrd.open_workbook("fileName.xls", formatting_info=True);
oldWbS = oldWb.sheet_by_index(0)
newWb = copy(oldWb);
newWs = newWb.get_sheet(0);
inserRowNo = 1
newWs.write(inserRowNo, 0, "value1");
newWs.write(inserRowNo, 1, "value2");
newWs.write(inserRowNo, 2, "value3");

for rowIndex in range(inserRowNo, oldWbS.nrows):
    for colIndex in range(oldWbS.ncols):
        newWs.write(rowIndex + 1, colIndex, oldWbS.cell(rowIndex, colIndex).value);
newWb.save('fileName.xls');
print "save with same name ok";

相关阅读

  • <em>Python</em> 操作<em>Excel</em>,怎么&

  • 乔山办公网excel表格制作
  • Python中一般使用xlrd(excel read)来读取Excel文件e69da5e887aa7a686964616f363,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取excel是不能对其进行
关键词不能为空
极力推荐
  • Excel年报图表制作指南-excel图表制作

  • excel图表制作,年终岁尾各种总结报告,表哥表姐们又要开始忙碌了。本篇不会详细介绍图表的制作方法,主要从图表类型选择,图表布局,图表配色三个方面来。

ppt怎么做_excel表格制作_office365_word文档_365办公网