乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何用<em>python</em>合并两个excel表格-python 合并excel,pyt

如何用<em>python</em>合并两个excel表格-python 合并excel,pyt

作者:乔山办公网日期:

返回目录:excel表格制作


import xlwt
f = xlwt.Workbook() #创建工作薄copy
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet
list = [1,2,3,4,5]
j = 0
for i in list:
sheet1.write(j,0,i) #循环写入 竖着写
j=j+1
f.save('text.xls')#保存文件


你把你的贴出来,看看有啥问题。最好能给个excel的模板数据

Created on Mon Mar 16 11:23:05 2015
@author: admin
"""
# 分批次运行,先得到e799bee5baa6e997aee7ad94e58685e5aeb9339books, 再运行,利用print 来debugimport pandas as pd
import numpy as np
import matplotlib as pl
import os
count=0
#ori_path='xx'
ori_path='C:\Python27\data\sample'
def get_xls_books(ori_path): # get target filename and dir of a pathtemp = list(os.walk(ori_path)) #tmp is list ,tmp[0]该目录文件路径和文件名, tmp[1]:子目录文件路径,文件名称book_path=[]
book_name=[]
root=temp[0][0]
for i in temp[0][2]:
if os.path.splitext(i)[1] == '.xlsx': #splite filenameobj_path=os.path.join(root,i)
book_path.append(obj_path)
book_name.append(os.path.splitext(i)[0])
return zip(book_path, book_name)
def get_xls_sheets(obj_path): #get all sheets of a object pathxls=pd.ExcelFile(obj_path)
sheet_names=xls.sheet_names
sheets=[xls.parse(i) for i in range(0,len(sheet_names)) ] # use parse can get dataframe saved into a listreturn zip(sheet_names,sheets)
books=get_xls_books(ori_path) # book paths list#xls=pd.ExcelFile(books[0])
#print xls.parse(0).columns
for book in books: # this loop get a bookbookname=book[1]
bookpath=book[0]
sheets=get_xls_sheets(bookpath) # get all sheets and names from book pathfor i in range(len(sheets)): #operate each sheet, is range(len) not lensheetname=sheets[i][0]
name=bookname+'-'+sheetname+'.'+'csv'
pd_sheet=sheets[i][1] # dataFrame
cols=pd_sheet.columns
pdf=pd_sheet[[cols[17],cols[18],cols[4],cols[6],cols[8],cols[10],cols[19],cols[16]]] # use emunerate functionpdf.columns=['area','patch','wangdian','operator','custid','servid','servtype','is_identify'] # renamepdf1=pdf[pdf['is_identify']=='是'] #filterpdf2=pdf1.drop('is_identify',1)
pdf2.to_csv(os.path.join(ori_path,name), header=None, index=None)

相关阅读

关键词不能为空
极力推荐

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