##I just took it when I saw it was useful, excerpted from: https://www.cnblogs.com/lingwang3/p/6416023.html
Python's reading and writing of Excel mainly include xlrd, xlwt, xlutils, openpyxl, and xlsxwriter.
1.xlrd is mainly used to read excel files
import xlrd
workbook = xlrd.open_workbook(u'Interesting daily data and trends.xls')
sheet_names= workbook.sheet_names()
for sheet_name in sheet_names:
sheet2 = workbook.sheet_by_name(sheet_name)
print sheet_name rows = sheet2.row_values(3) # Get the fourth row
cols = sheet2.col_values(1) # Get the content of the second column
print rows
print cols
2.xlwt is mainly used to write excel files
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
sheet.write(0,1,'test text')#Write content in the first column of row 0
wbk.save('test.xls')
3. xlutils combined with xlrd can achieve the purpose of modifying excel files
import xlrd
from xlutils.copy import copy
workbook = xlrd.open_workbook(u'Interesting daily data and trends.xls')
workbooknew = copy(workbook)
ws = workbooknew.get_sheet(0)
ws.write(3, 0, 'changed!')
workbooknew.save(u'interestingly loaded daily data and trend copy.xls')
4. openpyxl can read and write excel files
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter
workbook_ = load_workbook(u"Failed to retrieve new songs 1477881109469.xlsx")
sheetnames =workbook_.get_sheet_names() #Get the sheet name
print sheetnames
sheet = workbook_.get_sheet_by_name(sheetnames[0])
print sheet.cell(row=3,column=3).value
sheet['A1'] = '47'
workbook_.save(u"New song retrieval failed 1477881109469_new.xlsx")
wb = Workbook()
ws = wb.active
ws ['A1'] = 4
wb.save("Failed to retrieve new song.xlsx")
5.xlsxwriter can write excel files and add charts
import xlsxwriter
def get_chart(series):
chart = workbook.add_chart({'type': 'line'})
for ses in series:
name = ses["name"]
values = ses["values"]
chart.add_series({
'name': name,
'categories': 'A2:A10',
'values':values
})
chart.set_size({'width': 700, 'height': 350})
return chart
if __name__ == '__main__':
workbook = xlsxwriter.Workbook(u'H5 application center key data and trends.xlsx')
worksheet = workbook.add_worksheet(u"每日PV,UV")
headings = ['date', 'average']
worksheet.write_row('A1', headings)
index=0
for row in range(1,10):
for com in [0,1]:
worksheet.write(row,com,index)
index+=1
series = [{"name":"平均值","values":"B2:B10"}]
chart = get_chart(series)
chart.set_title ({'name': 'Daily page sharing data'})
worksheet.insert_chart('H7', chart)
workbook.close()
openpyxl
Python's reading and writing of Excel mainly include xlrd, xlwt, xlutils, openpyxl, and xlsxwriter.
1.xlrd is mainly used to read excel files
import xlrd
workbook = xlrd.open_workbook(u'Interesting daily data and trends.xls')
sheet_names= workbook.sheet_names()
for sheet_name in sheet_names:
sheet2 = workbook.sheet_by_name(sheet_name)
print sheet_name rows = sheet2.row_values(3) # Get the fourth row
cols = sheet2.col_values(1) # Get the content of the second column
print rows
print cols
2.xlwt is mainly used to write excel files
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
sheet.write(0,1,'test text')#Write content in the first column of row 0
wbk.save('test.xls')
3. xlutils combined with xlrd can achieve the purpose of modifying excel files
import xlrd
from xlutils.copy import copy
workbook = xlrd.open_workbook(u'Interesting daily data and trends.xls')
workbooknew = copy(workbook)
ws = workbooknew.get_sheet(0)
ws.write(3, 0, 'changed!')
workbooknew.save(u'interestingly loaded daily data and trend copy.xls')
4. openpyxl can read and write excel files
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter
workbook_ = load_workbook(u"Failed to retrieve new songs 1477881109469.xlsx")
sheetnames =workbook_.get_sheet_names() #Get the sheet name
print sheetnames
sheet = workbook_.get_sheet_by_name(sheetnames[0])
print sheet.cell(row=3,column=3).value
sheet['A1'] = '47'
workbook_.save(u"New song retrieval failed 1477881109469_new.xlsx")
wb = Workbook()
ws = wb.active
ws ['A1'] = 4
wb.save("Failed to retrieve new song.xlsx")
5.xlsxwriter can write excel files and add charts
import xlsxwriter
def get_chart(series):
chart = workbook.add_chart({'type': 'line'})
for ses in series:
name = ses["name"]
values = ses["values"]
chart.add_series({
'name': name,
'categories': 'A2:A10',
'values':values
})
chart.set_size({'width': 700, 'height': 350})
return chart
if __name__ == '__main__':
workbook = xlsxwriter.Workbook(u'H5 application center key data and trends.xlsx')
worksheet = workbook.add_worksheet(u"每日PV,UV")
headings = ['date', 'average']
worksheet.write_row('A1', headings)
index=0
for row in range(1,10):
for com in [0,1]:
worksheet.write(row,com,index)
index+=1
series = [{"name":"平均值","values":"B2:B10"}]
chart = get_chart(series)
chart.set_title ({'name': 'Daily page sharing data'})
worksheet.insert_chart('H7', chart)
workbook.close()
openpyxl