python excel模块在哪里加_Python如何利用win32com模块,在指定位置新建Excel? | python如何创建新文件...

python 将一个数组存到excle

就这一个数字的组合,这你看看这个组合的和这方面的软件介绍。

python怎么读写excel文件

最近用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 指定单元格内容

用xlwt和xlrd模块

xlrd获取指定格的内容

data=xlrd.open_workbook(你要读取的文档劲 文档名)

table=data.sheets()[0]:表示xls文件的第一个表格,[1]表示第二格

cell=table.cell(行,列).value #读取特定行特定列的内容

python 怎样对excle文件进行读写操作

分别进xlrd和xlwt文件中对setup.py进行安装,安装命令为setup.py install

进入python解释器,输入import xlwt,正常无报错即可

下面写的一个将Excle文件中的数据读取到普通文本中,和从普通文本写到Excel的一个互换程序:

[python] view plain copy

#encoding:utf8

import xlrd

import xlwt

class OperExcel():

#读取Excel表

def rExcel(self,inEfile,outfile):

rfile = xlrd.open_workbook(inEfile)

#创建索引顺序获取一个工作表

table = rfile.sheet_by_index(0)

#其他方式

#table = rfile.sheets()[0]

#table = rfile.sheet_by_name(u'Sheet1')

#获取整行,整列的值

table.row_values(0)

table.col_values(0)

#获取行数和列数

nrows = table.nrows - 1

ncols = table.ncols

#循环获取列表的数据

#for i in range(nrows):

# print table.row_values(i)

wfile = open(outfile,'w')

#获取第一列中的所有值

for i in range(nrows):

#table.cell(i,0).value获取某一单元格的值

wfile.write(table.cell(i,0).value.encode('utf8') '\n')

wfile.close()

#将数据写入Excel表

def wExcel(self,infile,outEfile):

rfile = open(infile,'r')

buf = rfile.read().split('\n')

rfile.close()

w = xlwt.Workbook()

sheet = w.add_sheet('sheet1')

for i in range(len(buf)):

print buf[i]

sheet.write(i,0,buf[i].decode('utf8'))

w.save(outEfile)

if __name__ == '__main__':

t = OperExcel()

t.rExcel('test.xls','test')

t.wExcel('test','1.xls')

Python如何将一个列表写入到一个excel表中的一列,求Python代码,谢啦!

import xlwt

f = xlwt.Workbook() #创作簿

sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet

l_=[1,2,3,4,5]

for i in range(len(l_)):

sheet1.write(0,i,i)#表格的行开始写。第一列,第二列。。。。

#sheet1.write(0,0,start_date,set_style('Times New Roman',220,True))

f.save('text.xls')#保存文

Python如何利用win32com模块,在指定位置新建Excel?

一般操作excel读xlrd模块,写入使用xlwt模块,修改使用xlutils模块。

根据你的需求,使用xlwt新建excel代码如下:

# -*- coding:utf-8 -*-

import xlwt

#新个excel文件

file = xlwt.Workbook()

#新建一个sheet

table = file.add_sheet('sheet1')

#写入数据table.write(行,列,value)

table.write(0,0,'test')

#保存文件

file.save('D:/demo.xls')

如何用python创建excel表格

以下代码调试:

import

xlrd#

打开

xls

文件book

=

xlrd.open_workbook("test.xls")print

"表单数量:",

book.nsheetsprint

"表单名称:",

book.sheet_names()#

获取第1个表单sh

=

book.sheet_by_index(0)print

u"表单

%s

%d

%d

"

%

(sh.name,

sh.nrows,

sh.ncols)print

"行第三列:",

sh.cell_value(1,

2)运行效果:

python怎么在excle最后一列增加?

效果:

import pandas as pd

# 读取文

data = pd.read_excel('./test.xls')

# 增加列,行数要相同,否则出错

data['gender'] = ['m', 'f', 'm']

print(data)

# 保存件

data.to_excel('./res.xls', index=False)

希望能帮到你。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值