『壹』 如何用python把多個excel文件自動合並到一個文件中
工作中經常用到Excel,很多事情都重復處理,比如每天的日報,每周的周報,各種數據表,這種固定的表其實都是重復性的勞動。最近這段時間一直在學python,但是斷斷續續的,眼高手低,看別人的代碼看的很懂,但是自己就是寫不出來,決定自己寫個小程序練練手,解決下身邊的實際問題,提高工作效率。
這個小腳本主要是把多個excel文件合並到一個文件中。網上搜索了下文章,有不少excel的python庫,最後選擇了適合python3的openpyxl庫,這個庫安裝很簡單,
pip install openpyxl
開發的環境是:mac/win +python3.5 +pycharm
廢話不多說,把代碼貼出來,請大家多指點下,我覺著代碼還可以更簡潔下,如果大家有好的優化方案麻煩留言指導下:
#coding=gbkimport openpyxlexcel_data=['qihu.xlsx','.xlsx']# new=openpyxl.load_workbook('all.xlsx')for excel_name in excel_data:
wb= openpyxl.load_workbook(excel_name,data_only=True)
sheet_name=wb.get_sheet_names()
# print(sheet_name)
for work in sheet_name:
nb = openpyxl.load_workbook('all.xlsx',data_only=True)
newsheet_name = nb.get_sheet_names()
if work in newsheet_name :
name = nb.get_sheet_by_name(work)
sheet = wb.get_sheet_by_name(work)
for i in range(1,sheet.max_row+1):
for j in range(1,sheet.max_column+1):
# 獲取整個sheet數據
data=sheet.cell(row=i,column=j).value
name.cell(column=j,row=i).value=data
else:
newsheet = nb.create_sheet(title=work)
name = nb.get_sheet_by_name(work)
sheet = wb.get_sheet_by_name(work)
for i in range(1,sheet.max_row+1):
for j in range(1,sheet.max_column+1):
# 獲取整個sheet數據
data=sheet.cell(row=i,column=j).value
name.cell(column=j,row=i).value=data
print(name)
nb.save('all.xlsx')
# print(newsheet_name)
『貳』 python操作excel問題
# -*- coding: utf-8 -*-#導入xlwt模塊import xlwt# 創建一個Workbook對象,這就相當於創建了一個Excel文件book = xlwt.Workbook(encoding='utf-8', style_compression=0)'''Workbook類初始化時有encoding和style_compression參數encoding:設置字元編碼,一般要這樣設置:w = Workbook(encoding='utf-8'),就可以在excel中輸出中文了。默認是ascii。當然要記得在文件頭部添加:#!/usr/bin/env python# -*- coding: utf-8 -*-style_compression:表示是否壓縮,不常用。'''#創建一個sheet對象,一個sheet對象對應Excel文件中的一張表格。# 在電腦桌面右鍵新建一個Excel文件,其中就包含sheet1,sheet2,sheet3三張表sheet = book.add_sheet('test', cell_overwrite_ok=True)# 其中的test是這張表的名字,cell_overwrite_ok,表示是否可以覆蓋單元格,其實是Worksheet實例化的一個參數,默認值是False# 向表test中添加數據sheet.write(0, 0, 'EnglishName') # 其中的'0-行, 0-列'指定表中的單元,'EnglishName'是向該單元寫入的內容sheet.write(1, 0, 'Marcovaldo')txt1 = '中文名字'sheet.write(0, 1, txt1.decode('utf-8')) # 此處需要將中文字元串解碼成unicode碼,否則會報錯txt2 = '馬可瓦多'sheet.write(1, 1, txt2.decode('utf-8')) # 最後,將以上操作保存到指定的Excel文件中book.save(r'e:\test1.xls') # 在字元串前加r,聲明為raw字元串,這樣就不會處理其中的轉義了。否則,可能會報錯
『叄』 求助用python從資料庫取數據動態生成表格的方法
一、可使用的第三方庫
python中處理excel表格,常用的庫有xlrd(讀excel)表、xlwt(寫excel)表、openpyxl(可讀寫excel表)等。xlrd讀數據較大的excel表時效率高於openpyxl,所以我在寫腳本時就採用了xlrd和xlwt這兩個庫。介紹及下載地址為:http://www.python-excel.org/ 這些庫文件都沒有提供修改現有excel表格內容的功能。一般只能將原excel中的內容讀出、做完處理後,再寫入一個新的excel文件。
二、常見問題
使用python處理excel表格時,發現兩個個比較難纏的問題:unicode編碼和excel中記錄的時間。
因為python的默認字元編碼都為unicode,所以列印從excel中讀出的中文或讀取中文名的excel表或sheet時,程序提示錯誤UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)。這是由於在windows中,中文使用了gb2312編碼方式,python將其當作unicode和ascii來解碼都不正確才報出的錯誤。使用VAR.encode('gb2312')即可解決列印中文的問題。(很奇怪,有的時候雖然能列印出結果,但顯示的不是中文,而是一堆編碼。)若要從中文文件名的excel表中讀取數據,可在文件名前加『u』表示將該中文文件名採用unicode編碼。
有excel中,時間和日期都使用浮點數表示。可看到,當『2013年3月20日』所在單元格使用『常規』格式表示後,內容變為『41353』;當其單元格格式改變為日期後,內容又變為了『2013年3月20日』。而使用xlrd讀出excel中的日期和時間後,得到是的一個浮點數。所以當向excel中寫入的日期和時間為一個浮點數也不要緊,只需將表格的表示方式改為日期和時間,即可得到正常的表示方式。excel中,用浮點數1表示1899年12月31日。
三、常用函數
以下主要介紹xlrd、xlwt、datetime中與日期相關的函數。
import xlrd
import xlwt
from datetime
def testXlrd(filename):
book=xlrd.open_workbook(filename)
sh=book.sheet_by_index(0)
print "Worksheet name(s): ",book.sheet_names()[0]
print 'book.nsheets',book.nsheets
print 'sh.name:',sh.name,'sh.nrows:',sh.nrows,'sh.ncols:',sh.ncols
print 'A1:',sh.cell_value(rowx=0,colx=1)
#如果A3的內容為中文
print 'A2:',sh.cell_value(0,2).encode('gb2312')
def testXlwt(filename):
book=xlwt.Workbook()
sheet1=book.add_sheet('hello')
book.add_sheet('word')
sheet1.write(0,0,'hello')
sheet1.write(0,1,'world')
row1 = sheet1.row(1)
row1.write(0,'A2')
row1.write(1,'B2')
sheet1.col(0).width = 10000
sheet2 = book.get_sheet(1)
sheet2.row(0).write(0,'Sheet 2 A1')
sheet2.row(0).write(1,'Sheet 2 B1')
sheet2.flush_row_data()
sheet2.write(1,0,'Sheet 2 A3')
sheet2.col(0).width = 5000
sheet2.col(0).hidden = True
book.save(filename)
if __name__=='__main__':
testXlrd(u'你好。xls')
testXlwt('helloWord.xls')
base=datetime.date(1899,12,31).toordinal()
tmp=datetime.date(2013,07,16).toordinal()
print datetime.date.fromordinal(tmp+base-1).weekday()