xls0-python3my使用python.pandas修改excel樣式
select 6006*0.618 *0.618 x,6006*0.618 xs from DUAL ;
select 6011*0.618 *0.618 x,6011*0.618 xs from DUAL ;
select 6015*0.618 *0.618 x,6015*0.618 xs from DUAL ;
select 6019*0.618 *0.618 x,6019*0.618 xs from DUAL ;
select 6024*0.618 *0.618 x,6024*0.618 xs from DUAL ;
declare
i number;
begin
for x in 5900..6222 loop
dbms_output.put_line(x||'--'||to_char(x*0.618,'9999.99')||'--'||round(x*0.618*0.618,3 ) );
end loop;
end;
/
SQL>
環境:python 3.6.8
>>> pd.read_excel('1.xlsx', sheet_name='Sheet2')
名字 等級 屬性1 屬性2 天賦
0 四九幻曦 100 自然 None 21
1 聖甲狂戰 100 戰斗 None 0
2 時空界皇 100 光 次元 27
我們在這里使用了pd.read_excel()
函數來讀取excel,來看一下read_excel()
這個方法的API,這里只截選一部分經常使用的參數:
pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None)
io
:很明顯, 是excel文件的路徑+名字字符串
(有中文的話python2
的老鐵需要使用decode()
來解碼成unicode字符串
)
例如:
>>> pd.read_excel('例子'.decode('utf-8))
sheet_name
:返回指定的 sheet
如果將sheet_name
指定為None
,則返回全表
如果需要返回多個表, 可以將sheet_name
指定為一個列表, 例如['sheet1', 'sheet2']可以根據
sheet
的名字字符串或索引來值指定所要選取的sheet
>>> # 如:
>>> pd.read_excel('1.xlsx', sheet_name=0)
>>> pd.read_excel('1.xlsx', sheet_name='Sheet1')
>>> # 返回的是相同的 DataFrame
name:如果沒有表頭, 可用此參數傳入列表做表頭
header:指定數據表的表頭,默認值為0, 即將第一行作為表頭
index_col:用作行索引的列編號或者列名,如果給定一個序列則有多個行索引。一般可以設定index_col=False
指的是pandas不適用第一列作為行索引。usecols:讀取指定的列, 也可以通過名字或索引值
>>> # 如:
>>> pd.read_excel('1.xlsx', sheet_name=1, usecols=['等級', '屬性1'])
>>> pd.read_excel('1.xlsx', sheet_name=1, usecols=[1,2])
>>> # 返回的是相同的 DataFrame
直到某一天泰格爾升了一級, 可以這樣改一下, 當然用.iloc
或.loc
對象都可以
>>> # 讀取文件
>>> data = pd.read_excel("1.xlsx", sheet_name="Sheet1")
>>> # 找到 等級 這一列,再在這一列中進行比較
>>> data['等級'][data['名字'] == '泰格爾'] += 1
>>> print(data)
LOOK!他升級了!!
>>> data
名字 等級 屬性1 屬性2 天賦
0 艾歐里婭 100 自然 冰 29
1 泰格爾 81 電 戰斗 16
2 布魯克克 100 水 None 28
現在我們將它保存
data.to_excel('1.xlsx', sheet_name='Sheet1', index=False, header=True)
index:默認為True
, 是否加行索引, 直接上圖吧!
左為False
, 右為True
header:默認為True
, 是否加列標, 上圖吧!
左為False
, 右為True
而io, sheet_name
參數用法同函數pd.read_excel()
如果我們多捕捉幾只或者多加幾種屬性怎么辦呢?這里給出參考:
新增列數據:
data['列名稱'] = [值1, 值2, ......]
>>> data['特性'] = ['瞬殺', 'None', '炎火']
>>> data
名字 等級 屬性1 屬性2 天賦 特性
0 艾歐里婭 100 自然 冰 29 瞬殺
1 泰格爾 80 電 戰斗 16 None
2 布魯克克 100 水 None 28 炎火
新增行數據,這里行的num為excel中自動給行加的id數值
data.loc[行的num] = [值1, 值2, ...], (注意與.iloc
的區別)
>>> data.loc[3] = ['小火猴', 1, '火', 'None', 31, 'None']
>>> data
名字 等級 屬性1 屬性2 天賦 特性
0 艾歐里婭 100 自然 冰 29 瞬殺
1 泰格爾 80 電 戰斗 16 None
2 布魯克克 100 水 None 28 炎火
3 小火猴 1 火 None 31 None
說完了增加一行或一列,那怎樣刪除一行或一列呢?可以使用.drop()
函數
>>> # 刪除列, 需要指定axis為1,當刪除行時,axis為0
>>> data = data.drop('屬性1', axis=1) # 刪除`屬性1`列
>>> data
名字 等級 屬性2 天賦 特性
0 艾歐里婭 100 冰 29 瞬殺
1 泰格爾 80 戰斗 16 None
2 布魯克克 100 None 28 炎火
3 小火猴 1 None 31 None
>>> # 刪除第3,4行,這里下表以0開始,並且標題行不算在類, axis用法同上
>>> data = data.drop([2, 3], axis=0)
>>> data
名字 等級 屬性2 天賦 特性
0 艾歐里婭 100 冰 29 瞬殺
1 泰格爾 80 戰斗 16 None
>>> # 保存
>>> data.to_excel('2.xlsx', sheet_name='Sheet1', index=False, header=True)
參考官網提供的API:http://pandas.pydata.org/pand...
使用python.pandas修改excel樣式
import pandas as pd
from datetime import datetime,timedelta
df = pd.read_clipboard() # 從粘貼板上讀取數據
t = datetime.now().date() - timedelta(days=1)
writer = pd.ExcelWriter('樣式%d%02d%02d.xlsx' %(t.year,t.month,t.day))
workbook = writer.book
fmt = workbook.add_format({"font_name": u"微軟雅黑"})
percent_fmt = workbook.add_format({'num_format': '0.00%'})
amt_fmt = workbook.add_format({'num_format': '#,##0'})
border_format = workbook.add_format({'border': 1})
note_fmt = workbook.add_format(
{'bold': True, 'font_name': u'微軟雅黑', 'font_color': 'red', 'align': 'left', 'valign': 'vcenter'})
date_fmt = workbook.add_format({'bold': False, 'font_name': u'微軟雅黑', 'num_format': 'yyyy-mm-dd'})
date_fmt1 = workbook.add_format(
{'bold': True, 'font_size': 10, 'font_name': u'微軟雅黑', 'num_format': 'yyyy-mm-dd', 'bg_color': '#9FC3D1',
'valign': 'vcenter', 'align': 'center'})
highlight_fmt = workbook.add_format({'bg_color': '#FFD7E2', 'num_format': '0.00%'})
l_end = len(df.index) + 2 # 表格的行數,便於下面設置格式
df.to_excel(writer, sheet_name=u'測試頁簽', encoding='utf8', header=False, index=False, startcol=0, startrow=2)
worksheet1 = writer.sheets[u'測試頁簽']
for col_num, value in enumerate(df.columns.values):
worksheet1.write(1, col_num, value, date_fmt1)
worksheet1.merge_range('A1:B1', u'測試情況統計表', note_fmt)
# 設置列寬
worksheet1.set_column('A:D', 30, fmt)
# 有條件設定表格格式:金額列
worksheet1.conditional_format('B3:E%d' % l_end, {'type': 'cell', 'criteria': '>=', 'value': 1, 'format': amt_fmt})
# 有條件設定表格格式:百分比
worksheet1.conditional_format('E3:E%d' % l_end,
{'type': 'cell', 'criteria': '<=', 'value': 0.1, 'format': percent_fmt})
# 有條件設定表格格式:高亮百分比
worksheet1.conditional_format('E3:E%d' % l_end,
{'type': 'cell', 'criteria': '>', 'value': 0.1, 'format': highlight_fmt})
# 加邊框
worksheet1.conditional_format('A1:E%d' % l_end, {'type': 'no_blanks', 'format': border_format})
# 設置日期格式
worksheet1.conditional_format('A3:A62', {'type': 'no_blanks', 'format': date_fmt})
writer.save()