如何利用python實現報表自動化?讓你更高效的完成工作內容

如果能夠實現報表自動化,那我們將節約不少的時間,更高效的完成工作內容。那麼,如何利用python實現報表自動化呢?本文將介紹xlwt 、xlrd、xlutils的常用功能,xlwt寫Excel時公式的應用以及xlwt寫入特定目錄來手把手帶大家實現報表自動化。


如何利用python實現報表自動化?讓你更高效的完成工作內容

1、python寫excel

(1)準備工作安裝xlwt :在終端中輸入pip install xlwt或者easy_install xlwt引入xlwt包 :

<code>

import

xlwt /<code>

(2)基礎教程新建工作簿&增加sheet: 新建一個工作簿,然後往裡添加sheet

<code>f = xlwt.Workbook()   
sheet1 = f.add_sheet(

u'sheet1'

, cell_overwrite_ok=

True

)/<code>

一個excel表格中可以添加多個sheet 往sheet中寫入內容:

sheet.write函數可以傳三個參數第i(參數1)第j(參數2)列存入內容(參數3)

<code>

sheet1

.write

(i, j,

'第i行第j列存放此內容'

, style)/<code>

這條語句實現的功能就是往第i行第j列存第三個參數的內容,第四個參數是樣式(如字體,背景),可以不傳第四個參數。

合併單元格並寫入內容:

<code>

sheet1

.write_merge

(x, x + m, y, y + n,

'內容'

, style)/<code>

這條y語句表示將[x:x+m]行[y:y+n]列的矩陣合併成一個單元格。存放第五個參數的#內容,同理,style參數可以不傳參 最後使用f.save(‘demo’)就可以把f保存到excel了

(3)實戰我們可以先新建一個工作簿,然後往裡添加兩個sheet,然後查看效果

<code> 

import

xlwt f = xlwt.Workbook() sheet1 = f.add_sheet(

u'表一'

, cell_overwrite_ok=

True

) sheet2 = f.add_sheet(

u'表二'

, cell_overwrite_ok=

True

) save(

'xlwt_tutorial'

)/<code>

我們開始往sheet中寫入內容,不傳入style參數先只使用write函數

<code>

import

xlwt f = xlwt.Workbook() sheet1 = f.add_sheet(

u'表一'

, cell_overwrite_ok=

True

) sheet2 = f.add_sheet(

u'表二'

, cell_overwrite_ok=

True

) row =

0

temp = [

u'姓名'

,

u'年齡'

,

u'學校'

,

u'專業'

]

for

pos,v

in

enumerate(temp): sheet1.write(row,pos,v) row +=

1

sheet1.write(row,

0

,

u'張三'

) sheet1.write(row,

1

,

18

) sheet1.write(row,

2

,

u'清華大學'

) row +=

1

sheet1.write(row,

0

,

u'李四'

) sheet1.write(row,

1

,

20

) sheet1.write(row,

2

,

u'北京大學'

) f.save(

'xlwt_tutorial'

)/<code>

這樣我們就建立了一個3行4列的表格。

(write函數行和列值都是從0開始的) 下面我們使用write_merge函數來合併單元格並寫入在f.save之前添加一行代碼:

<code>sheet1.write_merge(

1

,

2

,

3

,

3

,

u'漢語言文學'

)/<code>

將第2-3行第4列合併 2、pythonxd讀excel

(1)準備工作安裝xlrd :在終端中輸入pip install xlrd或者easy_install xlrd引入xlrd包 :

<code>

import

xlrd /<code>

(2)基礎教程&實戰打開一個Excel,然後輸出所有sheet的名字

<code> 

import

xlrd

import

uniout f = xlrd.open_workbook(

r'xlwt_tutorial'

)

print

f.sheet_names() 輸出:[u’表一’, u’表二’] /<code>

得到表格裡的所有的sheet

<code>

for

i in

range

(

len

(f.sheet_names())): sheet1 = workbook.sheet_by_index(i)/<code>

得到sheet中的內容

<code> 
f = xlrd.open_workbook(

r'xlwt_tutorial'

) sheet1 = f.sheet_by_index(

0

) sheet2 = f.sheet_by_name(

u'表二'

)

print

sheet1.name,sheet1.nrows,sheet1.ncols

print

sheet2.name,sheet2.nrows,sheet2.ncols 輸出為:表一

3

4

表二

0

0

print

sheet1.row_values(

1

)

print

sheet1.col_values(

2

) 輸出為:[u’張三’,

18.0

, u’清華大學’, u’漢語言文學’][u’學校’, u’清華大學’, u’北京大學’]

print

sheet1.cell(

1

,

0

).value

print

sheet1.cell(

1

,

1

).ctype 輸出為:張三

2

/<code>

3、xlutils 常用功能

(1)準備工作安裝xlutils :在終端中輸入pip install xlutils或者easy_install xlutils引入xlutils包 :

<code>

import

xlutils/<code>

(2)xlutils中copy功能我們可能會遇到一個問題,想對一個存儲好的Excel進行編輯***。但是xlrd是隻讀模式,不能進行編寫。而xlwt是隻寫模式,不能讀入Excel文件進行編輯。我們可以採用xlrd打開一個文檔,後採用xlutils中copy功能把文檔拷貝*,然後進行編輯即可。

<code>

import

xlrd

from

xlutils.copy

import

copy f = xlrd.open_workbook(

r'xlwt_tutorial'

) wb = copy(f) sheet1 = wb.get_sheet(

0

)

print

sheet1.name sheet1.write(

3

,

0

,

'change'

) wb.save(

'xlwt_tutorial'

) 輸出為:表一輸出的表格已經改變。/<code>

(4)xlwt寫Excel時公式的應用我們寫用xlwt寫一個表格

<code>coding=utf

-8

import

xlwt f = xlwt.Workbook() sheet1 = f.add_sheet(

u'得分統計'

, cell_overwrite_ok=

True

) mdict = {

"monkey"

:{

"writing"

:

80

,

"reading"

:

60

,

"speaking"

:

70

,

"listening"

:

60

},

"grape"

:{

"writing"

:

100

,

"reading"

:

80

,

"speaking"

:

70

,

"listening"

:

60

}} sheet1.write(

0

,

0

,

u'得分統計'

) sheet1.write(

1

,

0

,

u'書法得分'

) sheet1.write(

2

,

0

,

u'閱讀得分'

) sheet1.write(

3

,

0

,

u'演講得分'

) sheet1.write(

4

,

0

,

u'聽力得分'

) temp = [

'writing'

,

'reading'

,

'speaking'

,

'listening'

]

for

pos,name

in

enumerate(mdict): sheet1.write(

0

,pos+

1

,name)

for

p,v

in

enumerate(temp): sheet1.write(p+

1

,pos+

1

,mdict[name][v]) f.save(

'得分統計'

)/<code>

統計grape的總分和monkey的總分:在f.save之前加入代碼:

<code>sheet1.write(

5

,

0

,

u'總分統計'

)

for

i

in

range(len(mdict)): forstr = chr(

65

+i+

1

)+

'2+'

+chr(

65

+i+

1

)+

'3+'

+chr(

65

+i+

1

)+

'4+'

+chr(

65

+i+

1

)+

'5'

print

forstr sheet1.write(

5

,i+

1

,xlwt.Formula(forstr)) 輸出為: B2+B3+B4+B5 C2+C3+C4+C5/<code>

5、xlwt寫入特定目錄

由於代碼分層的緣故,使代碼整體框架優美。我們需要把文件寫入到特定目錄下。但是由於xlwt中沒有直接寫入到特定目錄的函數。

因此使用shutil.move函數來把文件MOV到特定目錄下:

<code> 

import

xlwt

import

os

import

shutil path =

'../sheet/'

isExists = os.path.exists(path)

if

not

isExists: os.makedirs(path) f = xlwt.Workbook() sheet1 = f.add_sheet(

u'得分統計'

, cell_overwrite_ok=

True

) mdict = {

"monkey"

:{

"writing"

:

80

,

"reading"

:

60

,

"speaking"

:

70

,

"listening"

:

60

},

"grape"

:{

"writing"

:

100

,

"reading"

:

80

,

"speaking"

:

70

,

"listening"

:

60

}} sheet1.write(

0

,

0

,

u'得分統計'

) sheet1.write(

1

,

0

,

u'書法得分'

) sheet1.write(

2

,

0

,

u'閱讀得分'

) sheet1.write(

3

,

0

,

u'演講得分'

) sheet1.write(

4

,

0

,

u'聽力得分'

) temp = [

'writing'

,

'reading'

,

'speaking'

,

'listening'

]

for

pos,name

in

enumerate(mdict): sheet1.write(

0

,pos+

1

,name)

for

p,v

in

enumerate(temp): sheet1.write(p+

1

,pos+

1

,mdict[name][v]) sheet1.write(

5

,

0

,

u'總分統計'

)

for

i

in

range(len(mdict)): forstr = chr(

65

+i+

1

)+

'2+'

+chr(

65

+i+

1

)+

'3+'

+chr(

65

+i+

1

)+

'4+'

+chr(

65

+i+

1

)+

'5'

print

forstr sheet1.write(

5

,i+

1

,xlwt.Formula(forstr)) f.save(

'得分統計'

) shutil.move(

u'得分統計'

, path)/<code>

以上就是利用python實現報表自動化的具體步驟,大家都掌握了嗎?如果你還沒入門Python,不理解如何利用python實現報表自動化。

如果你處於想學Python或者正在學習Python,Python的教程不少了吧,但是是最新的嗎?說不定你學了可能是兩年前人家就學過的內容,在這小編分享一波2020最新的Python教程。獲取方式,私信小編 “ 資料 ”,即可免費獲取哦!


分享到:


相關文章: