如何用python将数据写入excel表格
# 需安装 xlrd-0.9.2 和 xlutils-1.7.1 这两个模块
from xlwt import Workbook, Formula
import xlrd
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
sheet1.write(0,0,10)
sheet1.write(0,1,20)
sheet1.write(1,0,Formula('A1/B1'))
sheet2 = book.add_sheet('Sheet 2')
row = sheet2.row(0)
row.write(0,Formula('sum(1,2,3)'))
row.write(1,Formula('SuM(1;2;3)'))
row.write(2,Formula("$A$1 $B$1*SUM('ShEEt 1'!$A$1:$b$2)"))
book.save('formula.xls')
book = xlrd.open_workbook('formula.xls')
sheet = book.sheets()[0]
nrows=sheet.nrows
ncols=sheet.ncols
for i in range(nrows):
for j in range(ncols):
print (sheet.cell(i,j).value)
用Python导出工程文件两个子页里的数据成为两个exc...
from xlrd import open_workbook
wb=open_workbook(r'd:/222.xlsx')
tb=wb.sheets()[0]
data=[]
for r in range(tb.nrows):
val=[]
for c in range(tb.ncols):
val.append(tb.cell_value(r,c))
data.append(tuple(val))
print(data)
如何使用python将大量数据导出到Excel中的小技巧
首先下载win2com模块,在Python 3.1下运行一下代码即可,随意转载但希望注明:CD-one的实例 注解
from win32com.client import Dispatch
xlApp = Dispatch('Excel.Application')
xlApp.Visible = 1#显示
xlApp.Workbooks.Add()#添加工作本
#在(1,1)这个单元格中填入‘Python’这个字符串
xlApp.ActiveSheet.Cells(1,1).Value = 'Python'
xlSheet = xlApp.ActiveSheet
#在(2,1)这个单元格中填入‘3’这个数字
xlSheet.Cells(2,1).Value = 3
#把A2*2这个表达式填入(4,1)这个单元格
xlSheet.Cells(4,1).Formula = '=A2*2'
#输出A1到A5的值
xlSheet.Range("A1:A5").Value
#改变(1,1)这个单元格的背景色
xlSheet.Cells(1,1).Interior.ColorIndex = 3
如何用python将数据写入excel表格
python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库。
1、#读取Excel
import xlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #行数
ncols = table.ncols #列数
for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行数据
for item in rowValues:
print item
2、写Excel文件
'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;
isBold:true:粗字段,false:普通字体'''
def WriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#红色字体
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体
for svalue in rowValueList:
strValue = unicode(str(svalue),'utf-8')
if isBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i 1
'''写excel文件'''
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['标题1','标题2','标题3','标题4','总计']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
for i in xrange(1,11):
rowIndex = rowIndex 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)
style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
在设置上Excel单元格的背景色时,fore_colour 支持的颜色是有限的,仅支持一下颜色
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D"""
另外一种方式是 用pyExcelerator
from pyExcelerator import *# excel 第一行数据excel_headDatas = [u'发布时间', u'文章标题', u'文章链接', u'文章简介']
articles =[
{u'发布时间':u'2017年5月9日',
u'文章标题':u'Python项目实战教程:国内就能访问的google搜索引擎',
u'
u'文章简介':u'大家可以留言、想了解python那个方向的知识、不然我也不知道'},
{u'发布时间':u'2017年5月4日',
u'文章标题':u'对于学习Django的建议、你知道的有那些',
u'文章链接':',
u'文章简介':u'随着Django1.4第二个候选版的发布,虽然还不支持Python3,但Django团队已经在着手计划中,据官方博客所说,Django1.5将会试验性的支持python3'}
]# 定义excel操作句柄excle_Workbook = Workbook()
excel_sheet_name = time.strftime('%Y-%m-%d')
excel_sheet = excle_Workbook.add_sheet(excel_sheet_name)
index = 0#标题for data in excel_headDatas:
excel_sheet.write(0, index, data)
index = 1index = 1#内容for article in articles:
colIndex = 0 for item in excel_headDatas:
excel_sheet.write(index, colIndex, article[item])
colIndex = 1
index = 1#保存test.xlsx到当前程序目录excle_Workbook.save('test.xlsx')# db = mongoDB.mongoDbBase()# db.Get_information_stat()
python 如何读取excel文件 将每一行存为数组
python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库。
1、#读取Excel
import xlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #行数
ncols = table.ncols #列数
for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行数据
for item in rowValues:
print item
2、写Excel文件
'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;
isBold:true:粗字段,false:普通字体'''
def WriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#红色字体
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体
for svalue in rowValueList:
strValue = unicode(str(svalue),'utf-8')
if isBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i 1
'''写excel文件'''
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['标题1','标题2','标题3','标题4','总计']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
for i in xrange(1,11):
rowIndex = rowIndex 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)
style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
在设置上Excel单元格的背景色时,fore_colour 支持的颜色是有限的,仅支持一下颜色
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D"""
另外一种方式是 用pyExcelerator
from pyExcelerator import *# excel 第一行数据excel_headDatas = [u'发布时间', u'文章标题', u'文章链接', u'文章简介']
articles =[
{u'发布时间':u'2017年5月9日',
u'文章标题':u'Python项目实战教程:国内就能访问的google搜索引擎',
u'
u'文章简介':u'大家可以留言、想了解python那个方向的知识、不然我也不知道'},
{u'发布时间':u'2017年5月4日',
u'文章标题':u'对于学习Django的建议、你知道的有那些',
u'文章链接':',
u'文章简介':u'随着Django1.4第二个候选版的发布,虽然还不支持Python3,但Django团队已经在着手计划中,据官方博客所说,Django1.5将会试验性的支持python3'}
]# 定义excel操作句柄excle_Workbook = Workbook()
excel_sheet_name = time.strftime('%Y-%m-%d')
excel_sheet = excle_Workbook.add_sheet(excel_sheet_name)
index = 0#标题for data in excel_headDatas:
excel_sheet.write(0, index, data)
index = 1index = 1#内容for article in articles:
colIndex = 0 for item in excel_headDatas:
excel_sheet.write(index, colIndex, article[item])
colIndex = 1
index = 1#保存test.xlsx到当前程序目录excle_Workbook.save('test.xlsx')# db = mongoDB.mongoDbBase()# db.Get_information_stat()