作者:乔山办公网日期:
返回目录:excel表格制作
你这是要改变矩阵的纬度了,
改变维度要用reshape,把4×500变成1×2000
然后把三行合在一起好像是用vstack
这个过程有几个比较麻烦的问题,比如读取日期、读合并单元格内容7a64e58685e5aeb9333。下面先看看基本的操作:
首先读一个excel文件,有两个sheet,测试用第二个sheet,sheet2内容如下:
python 对 excel基本的操作如下:
?
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
33
34
35
# -*- coding: utf-8 -*-
import xlrd
import xlwt
from datetime import date,datetime
def read_excel():
# 打开文件
workbook = xlrd.open_workbook(r'F:\demo.xlsx')
# 获取所有sheet
print workbook.sheet_names() # [u'sheet1', u'sheet2']
sheet2_name = workbook.sheet_names()[1]
# 根据sheet索引或者名称获取sheet内容
sheet2 = workbook.sheet_by_index(1) # sheet索引从0开始
sheet2 = workbook.sheet_by_name('sheet2')
# sheet的名称,行数,列数
print sheet2.name,sheet2.nrows,sheet2.ncols
# 获取整行和整列的值(数组)
rows = sheet2.row_values(3) # 获取第四行内容
cols = sheet2.col_values(2) # 获取第三列内容
1 安装xlrd模块
pip install xlrd
2 读取Excel
# -*- coding: utf-8 -*-
import xlrd
import os,shutil
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)
def mycopyfile(srcfile,dstfile):
if not os.path.isfile(srcfile):
print "%s not exist!"%(srcfile)
else:
fpath,fname=os.path.split(dstfile) #分离文件名和路径
if not os.path.exists(fpath):
os.makedirs(fpath) #创建路7a686964616fe59b9ee7ad94331径
shutil.copyfile(srcfile,dstfile) #复制文件
print "copy %s -> %s"%( srcfile,dstfile)
dst='D:\\temp\\'
mycopyfile(srcfile,dstfile)
file_paths = data
for p in file_paths:
srcfile = p
dstfile = dst + p.splt('/')[-1]
mycopyfile(srcfile,dstfile)
数据——导入外部数据——导入数据——指定导入的文件——设定条件——ok