Now let's use Xlwings to operate Excel. I think it's easier to use than Pandas / XlswWriter, probably because the expression is like Excel. You need to install it by touching Excel directly.
Install xlwings
conda install xlwings
Install Excel. The version I'm using this time is Excel for mac 2011
# -*- coding: utf-8 -*-
import xlwings as xw
import matplotlib.pyplot as plt
#Create Excel New Book
wb = xw.Workbook()
#Set a value in a cell
xw.Range('A1').value = 'Foo 1'
#Get the value
str = xw.Range('A1').value
print(str)
#Set table data based on the specified cell
xw.Range('A1').value = [['Foo1','Foo2', 'Foo3'], [10, 20, 30]]
#Get table data based on the specified cell
table = xw.Range('A1').table.value
print(table)
#Get the specified range of data
table2 = xw.Range('A1:C2').value
print(table2)
#Specify a workbook or sheet
table3 = xw.Range('Shett1', 'A1:C2', wkb=wb).value
print(table3)
#Add a matplotlib graph(I can create an excel graph)
fig = plt.figure()
plt.plot([1,2,3,4,5])
plot = xw.Plot(fig)
plot.show('Plot1', left=xw.Range('D3').left, top=xw.Range('D3').top)
#Save
file_name = "xlwings_sample.xlsx"
wb.save(file_name)
#Read an existing file
wb2 = xw.Workbook(file_name)
xw.Range('A1').value = 'I wrote'
wb2.save(file_name)
Recommended Posts