openpyxl Excel XLSX 파일 읽기/쓰기

1705 단어
쓰기:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import  sys
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
from openpyxl.writer.excel import ExcelWriter 

# 
wb = Workbook()
# sheet
ws = wb.active
ws.title = "DefaultSheet"
# sheet  -  
ws1 = wb.create_sheet("NewSheet")
# sheet  -  
ws2 = wb.create_sheet("FirstSheet",0)
# sheet 
ws3 = wb["FirstSheet"]

# 
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Fill
from copy import copy
from openpyxl.styles import colors, Color

# 
font = Font(color='000000')
a1 = ws['A1']
a2 = ws['D4']

font2 = copy(font)
font2.bold = True

a1.font = font
a2.font = font2

# 
a1.value = 'hello'
a2.value = 'hi'

from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting import Rule
dxf = DifferentialStyle(font=Font(bold=True), fill=PatternFill(start_color='EE1111', end_color='EE1111'))
rule = Rule(type='cellIs', dxf=dxf, formula=["10"])
ws2.conditional_formatting.add('A1:F40', rule)

for i in xrange(1,10):
    index = 'B'+str(i)
    print index
    ws2.cell('B'+str(i)).value = 'TEST' + str(i)
    ws2.cell('B'+str(i)).font = Font(size=14)

ws2.oddHeader.left.color = "CC3366"

wb.save('/Users/wangpingyang/Practice/excel_demo.xlsx')

읽다:
baseXMLPath = '/Users/wangpingyang/Practice/excel_demo.xlsx'
wb = load_workbook(baseXMLPath)
ws = wbYesterday.get_sheet_by_name('Default')

좋은 웹페이지 즐겨찾기