A collection of Excel operations often used in Python

A collection of Excel operations often used in Python

Overview

Paste the value from this Excel In another Excel, run something called a power query Another one. .. .. .. Ahhhhhh There was something like that.

To be honest, the work you are doing in Excel can be implemented with a simple Python program. Numerical verification is over Situations that it is not realistic to rewrite all the things of the predecessor

At that time, I made a program that automates only the operation. The logic that is likely to be used later has been made into a class.

Usage environment

Python 3.9.0 Visual Studio Code 1.52.0 openpyxl xlwings pandas

Source code

I wonder if openpyxl, xlwings, pandas are in the right place


#Python Excel Manipulation Library
#Libraryize operations that are likely to occur

import xlwings as xw
import openpyxl

class lib_excel_ope:
    #Constructor No processing at this time
    def __init__(self):
        pass

    def exec_macro(self,xlsm_name:str,macro_name:str):
        #Macro execution xlwings
        wb = xw.Book(xlsm_name) #Open the book
        macro = wb.macro(macro_name)    #Get macro
        macro()                          #Run macro
        
        wb.save(xlsm_name)
        wb.close
        apps = xw.apps        #Returns an application execution environment management instance
        app = apps.active
        app.kill()


    def copy_cell(self,frompath:str,topath:str,fromsheet:str,tosheet:str,fromrow:int,torow:int,frmcol:int,tocol:int):
        #Copy and paste the specified sheet
        #How to specify the range is the condition that it is within the target range On the end condition side of python+ 1
        wb1 = openpyxl.load_workbook(frompath)
        wb2 = openpyxl.load_workbook(topath)
        ws1 = wb1[fromsheet]
        ws2 = wb2[tosheet]
     
        for rownum in range(fromrow, torow + 1):
            for colnum in range(frmcol, tocol + 1):
                cellstr = ws1.cell(row=rownum,column=colnum).coordinate
                ws2[cellstr] = ws1[cellstr].value
        
        wb2.save(topath)

    def check_effctive_cell(self,filename:str,sheetname:str,checkcol:int):
        #Check how valid the line is
        wb1 = openpyxl.load_workbook(filename)
        ws1 = wb1[sheetname]
        #For the time being, assume that MAX is up to 1000 lines
        for rownum in range(1, 1000):
            cellstr = ws1.cell(row=rownum,column=checkcol).coordinate
            if ws1[cellstr].value is None :
                return rownum - 1 #Valid line is one line before None
        wb1.close()

    def ifnone_round(self,cellvalue):
        if cellvalue is None:
            return cellvalue
        else:
            return round(cellvalue)

    #Desk tractor No processing at this time
    def __del__(self):
        pass


if __name__ == '__main__': 
    #Code for unit testing
    #Python Excel Manipulation Library
    exlopeobj = lib_excel_ope()


Recommended Posts

A collection of Excel operations often used in Python
Python scikit-learn A collection of predictive model tips often used in the field
Python scikit-learn A collection of predictive model tips often used in the field
Summary of Excel operations using OpenPyXL in Python
A collection of commands frequently used in server management
Perform Scala-like collection operations in Python
Generate a first class collection in Python
[Python] A memo of frequently used phrases (by myself) in Python scripts
Summary of methods often used in pandas
Display a list of alphabets in Python 3
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
Summary of processes often performed in Pandas 1 (CSV, Excel file related operations)
I want to color a part of an Excel string in Python
Draw a graph of a quadratic function in Python
Get the caller of a function in Python
Techniques often used in python short coding (Notepad)
Make a copy of the list in Python
Rewriting elements in a loop of lists (Python)
Make a joyplot-like plot of R in python
Output in the form of a python array
Code often used in Python / Django apps [prefectures]
Get a glimpse of machine learning in Python
A well-prepared record of data analysis in Python
File operations in Python
File operations in Python
Python programming in Excel
A personal memo of Pandas related operations that can be used in practice
Basic data frame operations written by beginners in a week of learning Python
A memorandum of method often used in machine learning using scikit-learn (for beginners)
Create a data collection bot in Python using Selenium
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
A reminder about the implementation of recommendations in Python
Take a screenshot in Python
Create a function in Python
Summary of python file operations
Summary of Python3 list operations
Create a dictionary in Python
Four arithmetic operations in python
Equivalence of objects in Python
Make a bookmarklet in Python
Wrapping git operations in Python
Image Processing Collection in Python
Draw a heart in Python
Implementation of quicksort in Python
Find out the apparent width of a string in python
Make a table of multiplication of each element in a spreadsheet (Python)
Commands often used in the development environment during Python implementation
Get the number of specific elements in a python list
Developed a library to get Kindle collection list in Python
How to develop in a virtual environment of Python [Memo]
[Note] Import of a file in the parent directory in Python
How to get a list of built-in exceptions in python
A memo of writing a basic function in Python using recursion
A set of script files that do wordcloud in Python3
Find the eigenvalues of a real symmetric matrix in Python
[Python] Creating a GUI tool that automatically processes CSV of temperature rise data in Excel
I tried to create a Python script to get the value of a cell in Microsoft Excel
A record of patching a python package
Maybe in a python (original title: Maybe in Python)
Pixel manipulation of images in Python