Operate Excel with Python (1)

Today's purpose

--Get the data written in the opened Excel and save it in another Excel.

Folder structure

root/  ├ code/ │ └ Execution sources  └ data/ └ Excel you want to open

Point-like thing

--Operate Excel

import

I will try it for the time being

--Excel operation --Open Excel --Existing file example: wb = openpyxl.load_workbook (targetExcel, data_only = True) --New file example: wb2 = openpyxl.Workbook () --targetExcel is the target Excel (with path) --Enter data_only = True because you want the calculation result in Excel. --Save --Example: wb2.save (newFileName) --newFileName is the file name --The same applies to saving an existing file. If it has the same name, it will be overwritten, and if it has a different name, it will be saved as a different name.

--Access Sheet in Excel. --Example 1: ws = wb [targetSheetName] --Example 2: ws = wb.worksheets [0] --targetSheetName is the sheet name --Even if the sheet name is unknown, you can access it by specifying the sheet number as shown in Example 2.

Summary

――It looks like you will have a hard time until you get used to it. .. I'll do my best.

reference

--I'll put an excel image and a straightforward code.

--"Sales.xlsx" image エクセルイメージ.png

The code I wrote

import sys
import os
import openpyxl

#It is not actual sales data.
#I don't want to write directly.
targetExcel = r"../data/Sales.xlsx"
targetSheetName = "Last month sales"
newFileName = "new.xlsx"
newSheetName = "test"
targetRow = 5
maxCellNum = 6

#Excel open
# data_only=True gives the result of the expression. Without it, the formula itself can be taken.
wb = openpyxl.load_workbook(targetExcel, data_only=True)

#Access data in excel
ws = wb[targetSheetName]

#An empty list
datalist = []

#Put the data of the target row into the list from the opened Excel
for num in range(maxCellNum):
    datalist.append(ws.cell(targetRow, num+1).value)
    print(datalist[num])

#Good new excel
wb2 = openpyxl.Workbook()
ws2 = wb2.worksheets[0]
ws2.title = newSheetName

#Set in the same position as the original excel
for num in range(maxCellNum):
    ws2.cell(targetRow, num+1).value = datalist[num]

#Mortise
wb2.save(newFileName)

Postscript: The title has been updated.

Recommended Posts

Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Excel with Python
Operate Kinesis with Python
Handle Excel with python
Operate Blender with Python
Operate TwitterBot with Lambda, Python
Let's run Excel with Python
[Note] Operate MongoDB with Python
solver> Link> Solve Excel Solver with python
Operate a receipt printer with python
Try to operate Facebook with Python
Create an Excel file with Python3
Let's play with Excel with Python [Beginner]
Operate ECHONET Lite appliances with Python
Handle Excel CSV files with Python
Excel aggregation with Python pandas Part 1
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Microcomputer with Python
Cast with python
Operate smartlife power supply with python (de-IFTTT)
Draw Nozomi Sasaki in Excel with python
Quickly create an excel file with Python #python
Convert Excel data to JSON with python
Excel aggregation with Python pandas Part 2 Variadic
[GCP] Operate Google Cloud Storage with Python
Try to operate Excel using Python (Xlwings)
[Easy Python] Reading Excel files with openpyxl
Create Excel file with Python + similarity matrix
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
[Easy Python] Reading Excel files with pandas
Excel table creation with Python [Progress management table]
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Operate Redmine using Python Redmine
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
Operate Filemaker from Python