excel 문서 쓰기

1576 단어
import openpyxl

'''
 
'''
wb = openpyxl.load_workbook('test.xlsx')
print(wb.get_sheet_names())
sheet = wb.active
print(sheet)
print(sheet.title)
#  Sheet 
sheet.title = 'Spam Bacon Eggs Sheet'
print(wb.get_sheet_names())
#  
wb.save('copy_test.xlsx')
print('---------------------')

'''
 , create_sheet() and remove_sheet() , 
create_sheet() Worksheet ,index ,title 
'''
test = openpyxl.Workbook()
print(test.get_sheet_names())
print(test.create_sheet())
print(test.get_sheet_names())
print(test.create_sheet(index=0,title='first sheet'))
print(test.get_sheet_names())
print(test.create_sheet(index=2,title='Middle sheet'))
print(test.get_sheet_names())
test.remove_sheet(test.get_sheet_by_name('Middle sheet'))
test.remove_sheet(test.get_sheet_by_name('Sheet1'))
print(test.get_sheet_names())
print('----------------')

'''
 
'''
sheet = wb.get_sheet_by_name('Sheet2')
sheet['A1'] = 'Hello world!'
print(sheet['A1'].value)
wb.save('ceshi.xlsx')
  • 결과는:
  • ---------------------
    ['Sheet']
    
    ['Sheet', 'Sheet1']
    
    ['first sheet', 'Sheet', 'Sheet1']
    
    ['first sheet', 'Sheet', 'Middle sheet', 'Sheet1']
    ['first sheet', 'Sheet']
    ----------------
    Hello world!
    

    좋은 웹페이지 즐겨찾기