| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

import xlrd

##################一、excel的读取操作xlrd######################

data =xlrd.open_workbook(r"E:\myexcel.xls")

#0.打开excel操作
table = data.sheets()[0]       #通过索引顺序获取
table = data.sheet_by_index(0) #通过索引顺序获取
table = data.sheet_by_name(u'Sheet1')#通过名称获取

#1. 获取excel sheet对象
table1 =data.sheets()[0]
table2=data.sheet_by_index(0)
table3=data.sheet_by_name(U"Sheet1")
print(table1)
print(table2)
print(table3)

#2. 获取sheet的行与列数量.
rows=table1.nrows
col =table1.ncols
print("行数为%s \n列数为%s"%(rows,col))

#3. 获取整行和整列的数据.
row =table1.row_values(0)
col =table1.col_values(2)
print(row)
print(col)

#4.获取单元格数据
cell_a1 =table1.cell_value(0,0)
cell_x =table1.cell_value(2,3) #(第三行，第四列数据)

print(cell_a1)
print(cell_x)

##################二、excel的写操作xlwt######################

#0.导入xlwt
import  xlwt

#1.创建workbook对象
workbook =xlwt.Workbook(encoding ="utf-8",style_compression=0)

#2.创建一个sheet对象,一个sheet对象对应excel文件中一张表格.

print(sheet)
#3.向表中添加数据.
#
sheet.write(0,0,"english_name111111111111")
# sheet.write(1,0,"helloworld")
#
# #4.保存.
workbook.save(r"2")

workbook.save(r"2")

workbook.save("2.xls")打开能看到已经生成了 sheet 2.

[此贴子已经被作者于2019-12-27 10:22编辑过]

sheet.write(0,0,"english_name111111111111")
sheet.write(1,0,"helloworld")

[此贴子已经被作者于2019-12-27 10:54编辑过]

sheet.write(0,0,"english_name111111111111")
sheet.write(1,0,"helloworld")

data =xlrd.open_workbook(r"E:\myexcel.xls")

#0.打开excel操作
sheet = data.sheets()[0]       #通过索引顺序获取
sheet.write(0,0,"english_name111111111111")
sheet.write(1,0,"helloworld")[/quote]
data.save(r"E:\myexcel.xls")

import xlrd
import xlutils.copy

data = xlrd.open_workbook(r'E:\myexcel.xls')
ws = xlutils.copy.copy(data)
table=ws.get_sheet(0)
table.write(0,3,'abc')
ws.save(r'E:\myexcel.xls')

pip --version
pip install xlrd
pip install xlwt
pip install xlutils
pip install pyinstaller
pip install https://
pyinstaller -F test.py
File "(.+)", line ([0-9]+)
[\t]*def[\t].+:

[此贴子已经被作者于2019-12-29 10:36编辑过]

• 7
• 1/1页
• 1