成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

python3.5操作officeExcel表(二)-創(chuàng)新互聯(lián)

#第二篇:
##openpyxl

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),冷水江企業(yè)網(wǎng)站建設(shè),冷水江品牌網(wǎng)站建設(shè),網(wǎng)站定制,冷水江網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,冷水江網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

Write a workbook:

from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter

wb = Workbook()

dest_filename = 'testexcel.xlsx'

ws1 = wb.active
ws1.title = "range names"

for row in range(1,40):
ws1.append(range(600))

ws2 = wb.create_sheet(title="Pi")

ws2['F5'] = 3.14

ws3 = wb.create_sheet(title="Data")
for row in range(10,20):
    for col in range(27,54):
      _ = ws3.cell(column=col,row=row,value="{0}".format(get_column_letter(col)))

print(ws3['AA10'].value)

wb.save(filename = dest_filename)

Read an existing workbook:

from openpyxl import load_workbook
wb = load_workbook(filename='testexcel.xlsx')
sheet_ranges = wb['range names']
print(sheet_ranges['D18'].value)

在load_workbook中可使用:
在讀取單元格時,guess_types將啟用或禁用(默認)類型推斷。

data_only控制具有公式的單元格是具有公式(默認)還是上次Excel讀取工作表時存儲的值。

keep_vba控制是否保留任何Visual Basic元素(默認)。如果他們被保留,他們?nèi)匀皇遣豢删庉嫷摹?/p>

警告:
openpyxl當(dāng)前不會讀取Excel文件中的所有可能項目,因此如果打開并保存相同名稱,圖像和圖表將從現(xiàn)有文件中丟失。

###使用數(shù)字格式(Using number formats):

import datetime
from openpyxl import Workbook

wb = Workbook()
ws = wb.active

ws['A1'] = datetime.datetime(2018,6,8)

print(ws['A1'].number_format)

wb.guess_types = True

ws['B1'] = '3.14%'
wb.guess_types = False

print(ws['B1'].value)
print(ws['B1'].number_format)

###使用公式(Using formulae):

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

ws["A1"] = "=SUM(1,1)"
wb.save("formula.xlsx")

openpyxl從不評估公式,但可以檢查公式的名稱:

from openpyxl.utils import FORMULAE
print("HEX2DEC" in FORMULAE)

注意:
如果要使用未知的公式,這些公式必須以_xlfn作為前綴。

###合并/取消合并單元格
(Merge / Unmerge cells)

合并單元格時,將從工作表中刪除左上角的所有單元格。
格式化合并單元格的信息,參看樣式合并單元格。

合并單元格:

from openpyxl.workbook import Workbook

wb = Workbook()
ws = wb.active

ws.merge_cells('A2:D2')
#A2:D2合并單元格
ws.merge_cells(start_row=2,start_column=1,end_row=4,end_column=4)
wb.save('table\\merge.xlsx')

取消合并單元格:

from openpyxl.workbook import Workbook

wb = Workbook()
ws = wb.active

ws.merge_cells('A2:D2')
#
ws.unmerge_cells('A2:D2')

ws.merge_cells(start_row=2,start_column=1,end_row=4,end_column=4)

ws.unmerge_cells(start_row=2,start_column=1,end_row=4,end_column=4)

wb.save('table\\merge.xlsx')

###插入圖像(Inserting an image)
from openpyxl import Workbook
from openpyxl.drawing.image import Image

wb = Workbook()
ws = wb.active
ws['A1'] = 'learning for me happy'

img = Image('images.PNG')

ws.add_image(img,'A1')
wb.save('image.xlsx')

###Fold columns(outline)折疊列(大綱)

import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet()
ws.column_dimensions.group('A','D',hidden=True)
wb.save('table\\group.xlsx')

###格式化單元格
使用樣式(Working with styles):
介紹(Introduction)

樣式用于在屏幕上顯示時更改數(shù)據(jù)的外觀。它們也用于確定數(shù)字的格式。

樣式可以應(yīng)用于以下幾個方面:

字體設(shè)置字體大小,顏色,下劃線等。 填充以設(shè)置圖案或顏色漸變 邊框在單元格上設(shè)置邊框,單元格對齊保護。
以下是默認值:請參考:
://openpyxl.readthedocs.io/en/2.5/styles.html#styling-merged-cells

from openpyxl.styles import PatternFill,Border,Side,Alignment,Protection

font = Font(name='Calibri',
size=11,
bold=False,
italic=False,
vertAlign=None,
underline='none',
strike=False,
color='FF000000',
)

fill = PatternFill(fill_type=None,
   start_color='FFFFFFFF',
   end_color='FF000000')
border = Border(left=Side(border_style=None,
  color='FF000000'),
right=Side(border_style=None,
  color='FF000000'),
top=Side(border_style=None,
 color='FF000000'),
bottom = Side(border_style=None,
  color='FF000000'),
diagonal=Side(border_style=None,
  color='FF000000'),
diagonal_direction=0,
outline=Side(border_style=None,
 color='FF000000'),
vertical=Side(border_style=None,
  color='FF000000'),
horizontal=Side(border_style=None,
color='FF000000')
)

alignment = Alignment(horizontal='general',
  vertical='bottom',
  text_rotation=0,
 wrap_text=False,
  shrink_to_fit=False,
  indent=0)

number_format = 'General'
protection = Protection(locked=True,hidden=False)

###單元格樣式和命名樣式(Cell Styles and Named Styles):

有兩種類型的樣式:單元格樣式和命名樣式,也稱為樣式模板。

####Cell Styles
單元格樣式在對象之間共享,一旦它們被分配,它們就不能被改變。這樣可以防止不必要的副作用,例如改變大量單元格的樣式,而不是只改變一個樣式。

from openpyxl.styles import colors
from openpyxl.styles import Font,Color
from openpyxl import Workbook

wb = Workbook()
ws = wb.active

a1 = ws['A1']
d4 = ws['D4']
ft = Font(color=colors.RED)

a1 = ws['A1']
d4 = ws['D4']
ft = Font(color=colors.RED)

a1.font = ft
d4.font = ft

a1.font.italic = True  #is not allowed

a1.font = Font(color=colors.RED,italic=True)

Copying styles(復(fù)制樣式):
樣式也可以被復(fù)制.

from openpyxl.styles import Font
from copy import copy

ft1 = Font(name='Arial',size=14)
ft2 = copy(ft1)
ft2.name = "Tahoma"
print(ft1.name)
print(ft2.name)
print(ft2.size)

####Basic Font Colors(基本字體顏色)
顏色通常是RGB或aRGB的十六進制值。顏色模塊包含一些方便的常量.

from openpyxl.styles import Font
from openpyxl.styles.colors import RED

font = Font(color=RED)
font = Font(color="FFBB00")

還支持傳統(tǒng)索引顏色以及主題和色彩.

from openpyxl.styles.colors import Color

c = Color(indexed=32)
print(c)
c = Color(theme=6,tint=0.5)
print(c)

####應(yīng)用樣式(Applying Styles):
樣式直接應(yīng)用于單元格.

from openpyxl.workbook import Workbook
from openpyxl.styles import Font,Fill

wb = Workbook()
ws = wb.active
c = ws['A1']
c.font = Font(size=12)

wb.save('table\\style.xlsx')

樣式還可以應(yīng)用于列和行,但請注意,這僅適用于文件關(guān)閉后創(chuàng)建的單元格(在Excel中)。如果您想將樣式應(yīng)用于整個行和列,那么您必須將樣式應(yīng)用于每個單元格。這是對文件格式的限制:

col = ws.column_dimensions['A']
col.font = Font(bold=True)
row = ws.row_dimensions[1]
row.font = Font(underline="single")

####樣式合并單元格(Styling Merged Cells):
有時候你想格式化一系列單元格,就好像它們是單個對象一樣。 Excel假裝可以通過合并單元格(刪除除左上角單元格以外的所有單元格)然后重新創(chuàng)建這些單元格來應(yīng)用偽類型。代碼參考:
https://openpyxl.readthedocs.io/en/2.5/styles.html#styling-merged-cells

from openpyxl.styles import Border,Side,PatternFill,Font,GradientFill,Alignment
from openpyxl import Workbook

def style_range(ws,cell_range,border=Border(),fill=None,font=None,alignment=None):
"""
App styles to a range of cells as if they were a single cell.
:param ws:
:param cell_range:
:param border:
:param fill:
:param font:
:param alignment:
:return:
"""

top = Border(top=border.top)
left = Border(left=border.left)
right = Border(right=border.right)
bottom = Border(bottom=border.bottom)

first_cell = ws[cell_range.split(":")[0]]
if alignment:
   ws.merge_cells(cell_range)
    first_cell.alignment = alignment

rows = ws[cell_range]
if font:
    first_cell.font = font

for cell in rows[0]:
    cell.border = cell.border + top
for cell in rows[-1]:
    cell.border = cell.border + bottom

for row in rows:
    L = row[0]
    r = row[-1]
    L.border = L.border + left
    r.border = r.border + right
    if fill:
        for c in row:
            c.fill = fill

wb = Workbook()
ws = wb.active
my_cell = ws['B2']
my_cell.value = "My Cell"

thin = Side(border_,color = "000000")
double = Side(border_,color="ff0000")

border = Border(top=double,left=thin,right=thin,bottom=double)

fill = PatternFill("solid",fgColor="DDDDDD")
fill = GradientFill(stop=("000000","FFFFFF"))

font = Font(b=True,color="FF0000")
al = Alignment(horizontal="center",vertical="center")

style_range(ws,'B2:F4',border=border,fill=fill,font=font,alignment=al)

wb.save("table\\styled.xlsx")

####編輯頁面設(shè)置(Edit Page Setup):

from openpyxl.workbook  import Workbook

wb = Workbook()
ws = wb.active

ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID
ws.page_setup.fitToHeight = 0
ws.page_setup.fitToWidth = 1
wb.save("table\\styledone.xlsx")

####命名樣式(Named Styles):
與單元格樣式相比,命名樣式是可變的。當(dāng)您想要將格式一次應(yīng)用到很多不同的單元格時,它們是有意義的。 NB。一旦您為單元格指定了命名樣式,對樣式的其他更改將不會影響單元格。
一旦一個已命名的樣式被注冊到一個工作簿中,它就可以簡單地通過名稱來引用。

####創(chuàng)建一個命名樣式(Creating a Named Style):

from openpyxl.styles import NamedStyle,Font,Border,Side
highlight = NamedStyle(name="highlight")
highlight.font = Font(bold=True,size=20)
bd = Side(style='thick',color="000000")
highlight.border = Border(left=bd,top=bd,right=bd,bottom=bd)

一旦創(chuàng)建了命名樣式,它就可以在工作簿中注冊.

wb.add_named_style(highlight)

但是,第一次將它們分配給單元格時,命名樣式也會自動注冊:

ws['A1'].style = highlight

一旦注冊,使用名稱分配樣式:

ws['D5'].style = 'highlight'

例如:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
wb.add_named_style(highlight)
ws['A1'].style = highlight

ws['D5'].style = 'highlight'

wb.save("table\\styles.xlsx")

####使用內(nèi)置樣式(Using builtin styles):
該規(guī)范包括一些內(nèi)置的樣式,也可以使用。不幸的是,這些樣式的名稱以其本地化形式存儲。 openpyxl只會識別英文名稱,而且只能寫在這里。這些如下:

數(shù)字格式(Number formats):

‘Comma’
‘Comma [0]’
‘Currency’
‘Currency [0]’
‘Percent’

信息化(Informative):

‘Calculation’
‘Total’
‘Note’
‘Warning Text’
‘Explanatory Text’

文字樣式(Text styles):

‘Title’
‘Headline 1’
‘Headline 2’
‘Headline 3’
‘Headline 4’
‘Hyperlink’
‘Followed Hyperlink’
‘Linked Cell’

比較(Comparisons):

‘Input’
‘Output’
‘Check Cell’
‘Good’
‘Bad’
‘Neutral’

強調(diào)(Highlights):
‘Accent1’
‘20 % - Accent1’
‘40 % - Accent1’
‘60 % - Accent1’
‘Accent2’
‘20 % - Accent2’
‘40 % - Accent2’
‘60 % - Accent2’
‘Accent3’
‘20 % - Accent3’
‘40 % - Accent3’
‘60 % - Accent3’
‘Accent4’
‘20 % - Accent4’
‘40 % - Accent4’
‘60 % - Accent4’
‘Accent5’
‘20 % - Accent5’
‘40 % - Accent5’
‘60 % - Accent5’
‘Accent6’
‘20 % - Accent6’
‘40 % - Accent6’
‘60 % - Accent6’
‘Pandas’

有關(guān)內(nèi)建樣式的更多信息:
openpyxl.styles.builtins

如有錯誤之處,請指正。

參考:
https://openpyxl.readthedocs.io/en/latest/

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

標(biāo)題名稱:python3.5操作officeExcel表(二)-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://jinyejixie.com/article16/dcjcdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站設(shè)計App開發(fā)、動態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
阜新市| 德保县| 兴仁县| 广宁县| 花莲县| 苍山县| 安阳市| 微山县| 富阳市| 乡宁县| 和田市| 彝良县| 孟津县| 怀宁县| 张家川| 色达县| 阳谷县| 吴旗县| 长丰县| 雷山县| 东乌珠穆沁旗| 安远县| 屯门区| 内黄县| 巩留县| 金溪县| 北海市| 辽中县| 芜湖市| 万安县| 余干县| 临沂市| 宜丰县| 永嘉县| 镇江市| 乌拉特前旗| 噶尔县| 孝昌县| 黔江区| 南川市| 包头市|