[Python] Implemented automation in excel file copying work

Automation with python

This time, when the intern changed the existing Excel file to the form he wanted to use this time, he automated it using the python library, so I will write it here.

Thing you want to do

I already have personal information in multiple spreadsheets, but it's a hassle to pull one by one from the child each time, so I put that information together in one spreadsheet and handed it to the client at once. It's easier to do. In that case, it is necessary to copy the specified parts to a new spreadsheet one by one, but the manual work is quite troublesome. So I want to automate it with python.

Library to use

openpyxl (https://note.nkmk.me/python-openpyxl-usage/)

algorithm

--Create one array by extracting the specified value for each sheet --Put that array in a new array to create a two-dimensional array --Write based on the two-dimensional array

sample.py


def write_list_2d(sheet, l_2d, start_row, start_col):
    for y, row in enumerate(l_2d):
        for x, cell in enumerate(row):
            sheet.cell(row=start_row + y,
                       column=start_col + x,
                       value=l_2d[y][x])

l_2d = [['four', 41, 42, 43], ['five', 51, 52, 53]]

write_list_2d(sheet, l_2d, 5, 1)

Caution

Don't forget to save at the end. If you do not do this, it will not be reflected.

sample.py


wb_to.save('hoge.xlsx')

Use of regular expressions

If you want to further divide the extracted characters, use a regular expression. What I wanted to do this time is 「hogehogehoge(fugafuga)」 I wanted to divide it into two parts, one in parentheses and the other. The conclusion is as follows.

sample.py


list = re.match(r"(?P<comment>.*?)(?:[\((](?P<name>.*?)[)\)])?$",sentence)
            temp.append(list['comment'])
            temp.append(list['name'])

Recommended Posts

[Python] Implemented automation in excel file copying work
File operations in Python
Implemented SimRank in Python
UI Automation in Python
File operations in Python
Python programming in Excel
Implemented Shiritori in Python
[Work efficiency] How to change file names in Python
Download the file in Python
UI Automation Part 2 in Python
Sudoku solver implemented in Python 3
6 Ball puzzle implemented in python
[Python] How to change EXCEL file saved in xlsb to xlsx
Convert Excel file to text in Python for diff purposes
Use jinja2 template in excel file
File / folder path manipulation in Python
Implemented image segmentation in python (Union-Find)
Save the binary file in Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Linebot creation & file sharing in Python
Create an Excel file with Python3
Implemented label propagation method in Python
Implemented Perceptron learning rules in Python
ORC, Parquet file operations in Python
[Automation with python! ] Part 2: File operation
web coder tried excel in Python
Implemented in 1 minute! LINE Notify in Python
Get the formula in an excel file as a string in Python
Exclusive control with lock file in Python
Implemented in Python PRML Chapter 7 Nonlinear SVM
Draw Nozomi Sasaki in Excel with python
Convert psd file to png in Python
Quickly create an excel file with Python #python
How to work with BigQuery in Python
Write O_SYNC file in C and Python
Read the file line by line in Python
Read the file line by line in Python
I implemented Cousera's logistic regression in Python
Exclusive file access between processes in Python
Create Excel file with Python + similarity matrix
Open an Excel file in Python and color the map of Japan
[GPS] Create a kml file in Python
Implemented in Python PRML Chapter 5 Neural Networks
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
To work with timestamp stations in Python
Implemented in Python PRML Chapter 1 Bayesian Inference
[Python] Read the specified line in the file
[Automation] Read mail (msg file) with Python
[Improve work efficiency with Python] Request: Make all Excel PDFs in this folder.
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
Create a GIF file using Pillow in Python
I implemented Robinson's Bayesian Spam Filter in python
File DL, byte value and delete in Python3
Import Excel file from Python (register to DB)
[Automation] Extract the table in PDF with Python
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
Implemented memoization recursion and exploration in Python and Go
[Python] How to read excel file with pandas
Automatically create word and excel reports in python