Excel graph creation using python xlwings

comment

Please refer to the graph creation using xlwings. I would be happy if you pointed out that it is better to do this. If you use api, you can use vba code → If you don't understand, you can check it with "Record macro" in Excel. test.xlsx is a blank workbook.

environment

python 3.7 xlwings 0.18.0

Sample code

import xlwings as xw
from xlwings.constants import AxisType

DATA_NUM = 10
#Reading Excel workbook
wb = xw.Book('test.xlsx')

#Insert data
data_x = list(x for x in range(DATA_NUM))
data_y1 = list(x*2 for x in range(DATA_NUM))
data_y2 = list(x*10-50 for x in range(DATA_NUM))

xw.Range('A1').value = 'x'
xw.Range('B1').value = 'y1'
xw.Range('C1').value = 'y2'

cell_x = xw.Range('A2')
cell_y1 = xw.Range('B2')
cell_y2 = xw.Range('C2')
for cnt in range(DATA_NUM):
    cell_x.value = data_x[cnt]
    cell_y1.value = data_y1[cnt]
    cell_y2.value = data_y2[cnt]

    cell_x = cell_x.offset(1, 0)
    cell_y1 = cell_y1.offset(1, 0)
    cell_y2 = cell_y2.offset(1, 0)

#Insert graph
chart = xw.Chart()
#Adjustment of graph position and size
chart.left = 200
chart.top = 10
chart.width = 300
chart.height = 200
#Graph type setting
chart.chart_type = 'xy_scatter_lines_no_markers'
#Data range setting
chart.set_source_data(xw.Range('A1:C11'))
#Change x-axis position to bottom(xlminimum=4 ← Please tell me who knows how to read VBA constants)
# api[1]Please tell me who understands the meaning of 1. If you set it to 1 for the time being, it will work ...
chart.api[1].Axes(AxisType.xlValue).Crosses = 4
#Changed the axis scale to inward(xlInside=2)
chart.api[1].Axes(AxisType.xlCategory).MajorTickMark = 2
chart.api[1].Axes(AxisType.xlValue).MajorTickMark = 2

Recommended Posts

Excel graph creation using python xlwings
Run a python script from excel (using xlwings)
[Python] Introduction to graph creation using coronavirus data [For beginners]
GUI creation in python using tkinter 2
GUI creation in python using tkinter part 1
Excel table creation with Python [Progress management table]
[Note] Execute Python code from Excel (xlwings)
Start using Python
Scraping using Python
Excel with Python
Recent ranking creation using Qiita API with Python
Summary of Excel operations using OpenPyXL in Python
Run Python from Excel VBA with xlwings & tutorial supplement
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Data analysis using Python 0
Graph drawing in python
Run python from excel
Data cleaning using Python
Slack chatbot creation Python
[Python] How to use the graph creation library Altair
Using Python #external packages
[Python] Using Line API [1st Creation of Beauty Bot]
Chat creation using sockets
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Draw graph in python
Try using Tweepy [Python2.7]
Operate Excel with Python (1)
Operate Excel with Python (2)
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Output Excel data in separate writing using Python3 + xlrd + mecab
A memo when creating a directed graph using Graphviz in Python
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
Graph time series data in Python using pandas and matplotlib
Python notes using perl-ternary operator
Flatten using Python yield from
Save images using python3 requests
Graph Excel data with matplotlib (1)
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Try using Kubernetes Client -Python-
Python notes using perl-special variables
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Graph Excel data with matplotlib (2)
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)