Excel X Python The fastest way to work

The era of VBA seems to be over

Recently, a book called "Excel X Python Fastest Work Technique" was sold as of the end of December 2019, and has won the first place in Amazon's spreadsheet book ranking. It's amazing because it sells better than that Excel's strongest textbook! (The spreadsheet category is a fairly popular category) Amazon Link

The core of this book is that ** doing something like VBA from Python to Excel ** is what the fastest work technique is. This time, for my own learning, I will introduce how to operate Excel workbooks from Python learned from this book and do VBA-like things.

This book consistently uses a library called ** openpyxl ** to operate Excel.

Load the workbook. (New workbook)

import openpyxl
lwb = openpyxl.Workbook()
lsh = lwb.active #Load sheet for writing to cell

Load Excel that has already been created

import openpyxl
wb = openpyxl.load_workbook('file name.xlsx')

Save the document

import openpyxl
wb = openpyxl.load_workbook('file name.xlsx')
wb.save(filename = 'sample_book.xlsx')

Write to cell

import openpyxl
lwb = openpyxl.Workbook()
lsh = lwb.active #Load sheet for writing to cell
lsh.cell(Number of lines,Number of columns).value = "hogehoge"

Use charts (line charts)

I will put the code of the citation source Source link

from datetime import date

from openpyxl import Workbook
from openpyxl.chart import (
    LineChart,
    Reference,
)
from openpyxl.chart.axis import DateAxis

wb = Workbook()
ws = wb.active

rows = [
    ['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
    [date(2015,9, 1), 40, 30, 25],
    [date(2015,9, 2), 40, 25, 30],
    [date(2015,9, 3), 50, 30, 45],
    [date(2015,9, 4), 30, 25, 40],
    [date(2015,9, 5), 25, 35, 30],
    [date(2015,9, 6), 20, 40, 35],
]

for row in rows:
    ws.append(row)

c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

# Style the lines
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline

s1.graphicalProperties.line.noFill = True

s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"
s2.graphicalProperties.line.width = 100050 # width in EMUs

s2 = c1.series[2]
s2.smooth = True # Make the line smooth

ws.add_chart(c1, "A10")

from copy import deepcopy
stacked = deepcopy(c1)
stacked.grouping = "stacked"
stacked.title = "Stacked Line Chart"
ws.add_chart(stacked, "A27")

percent_stacked = deepcopy(c1)
percent_stacked.grouping = "percentStacked"
percent_stacked.title = "Percent Stacked Line Chart"
ws.add_chart(percent_stacked, "A44")

# Chart with date axis
c2 = LineChart()
c2.title = "Date Axis"
c2.style = 12
c2.y_axis.title = "Size"
c2.y_axis.crossAx = 500
c2.x_axis = DateAxis(crossAx=100)
c2.x_axis.number_format = 'd-mmm'
c2.x_axis.majorTimeUnit = "days"
c2.x_axis.title = "Date"

c2.add_data(data, titles_from_data=True)
dates = Reference(ws, min_col=1, min_row=2, max_row=7)
c2.set_categories(dates)

ws.add_chart(c2, "A61")

wb.save("line.xlsx")

Load multiple Excel

import openpyxl
import pathlib
path = pathlib.Path('Directory name/')
for path_obj in path.iterdir():
    wb = openpyxl.load_workbook('file name.xlsx')
    for sh in wb:
        #Operation for each sheet

Change log

--2019/12/30 Newly created

Recommended Posts

Excel X Python The fastest way to work
The fastest way for beginners to master Python
The fastest way to try EfficientNet
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
The fastest way to get camera images regularly with python opencv
The easiest way to synthesize speech with python
The easiest way to use OpenCV with python
[Algorithm x Python] How to use the list
Introduction to Python with Atom (on the way)
What is the fastest way to create a reverse dictionary in python?
[Python] Another way to import
Convert python 3.x code to python 2.x
Probably the easiest way to create a pdf with Python3
Leave the troublesome processing to Python
In the python command python points to python3.8
[Python] How to import the library
The easiest way to make Flask
The easiest way to try PyQtGraph
[Python] Change the alphabet to numbers
An easy way to hit the Amazon Product API in Python
Easy way to round off to the nearest whole number with python3
[Circuit x Python] How to enable the linear circuit analysis package Lcapy
Excel macro (VBA) x Python scraping to automate growth stock search
I tried to improve the efficiency of daily work with Python
Contents of reading VBA x Python fastest work technique Memo chapter1
How to display bytes in the same way in Java and Python
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
[Blender x Python] Let's master the material !!
What I read about VBA x Python fastest work technique Memo chapter3
[EV3 x Python] Stream the camera image to your PC using mjpg-streamer.
Convert Excel data to JSON with python
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
How to work with BigQuery in Python
How to erase Python 2.x on Mac.
Easy way to use Wikipedia in Python
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Try to operate Excel using Python (Xlwings)
Python amateurs try to summarize the list ①
Just migrate the Discord.py 0.X echobot to 1.X
In search of the fastest FizzBuzz in Python
What I read about VBA x Python fastest work technique Memo chapter2
To work with timestamp stations in Python
A way to understand Python duck typing
[Blender x Python] How to use modifiers
The road to compiling to Python 3 with Thrift
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
About the point that python 3.x got stuck due to an error due to caching_sha2_password
The right way of thinking for testing, dedicated to projects where testing doesn't work
How to use the C library in Python
Edit Excel from Python to create a PivotTable
Python constants like None (according to the reference)
The easiest way to get started with Django
Convert "number" of excel date to python datetime
Easy way to use Python 2.7 on Cent OS 6
Try to solve the Python class inheritance problem
[Python] How to change the date format (display format)
Just add the python array to the json data