Try to operate Excel using Python (Xlwings)

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.

Preparation

Install xlwings

conda install xlwings

Install Excel. The version I'm using this time is Excel for mac 2011

Create and write an Excel file

# -*- 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

#Read an existing file
wb2 = xw.Workbook(file_name)
xw.Range('A1').value = 'I wrote'
wb2.save(file_name)

Recommended Posts

Try to operate Excel using Python (Xlwings)
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Excel graph creation using python xlwings
Operate Redmine using Python Redmine
Try using Tweepy [Python2.7]
Operate Excel with Python (1)
Operate Excel with Python (2)
Run a python script from excel (using xlwings)
[Python] Try using Tkinter's canvas
Try to understand Python self
(Python) Try to develop a web application using Django
Try using Kubernetes Client -Python-
Try to operate DB with Python and visualize with d3
Post to Twitter using Python
Try to extract high frequency words using NLTK (python)
Try to make it using GUI and PyQt in Python
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
How to install python using anaconda
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try converting cloudmonkey CLI to python3 -1
Try to get statistics using e-Stat
Try using Python argparse's action API
Try using the Python Cmd module
Try using Leap Motion in Python
Try using Amazon DynamoDB from Python
Try python
Try to analyze online family mahjong using Python (PART 1: Take DATA)
Try to operate the database using Python's ORM Peewee (August 2019 version)
Try to log in to Netflix automatically using python on your PC
Try to reproduce color film with Python
Try logging in to qiita with Python
Try using the Wunderlist API in Python
From Python to using MeCab (and CaboCha)
Introduction to Discrete Event Simulation Using Python # 1
Try using the Kraken API in Python
Try using Python with Google Cloud Functions
Try to detect fusion movement using AnyMotion
Try using Junos On-box Python # 2 Commit Script
Log in to Slack using requests in Python
Dump BigQuery tables to GCS using Python
Python amateurs try to summarize the list ①
Introduction to Discrete Event Simulation Using Python # 2
Try using Junos On-box Python # 1 Op Script
[Note] Execute Python code from Excel (xlwings)
Try to download Youtube videos using Pytube
[Python] [Excel] Operate an Excel sheet from Python using openpyxl (using a test sheet as an example)
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Try to poke DB on IBM i with python + JDBC using JayDeBeApi
Try using Tkinter
Edit Excel from Python to create a PivotTable
Updated to Python 2.7.9
First steps to try Google CloudVision in Python
Environment construction procedure to operate chrome without installing python on Windows (using selenium)
Try sending Metrics to datadog via python, DogStatsD
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try using django-import-export to add csv data to django
Try using docker-py