Self-taught Python: Splitting excel Tables according to Conditions
Use the following statement:
Import xlwings as xw.
Import pandas as pd
app = xw。 App(visible =True,add_book =False)
workbook = app . books . open(' e:\ \ table \ \ worksheet . xlsx ')。
Worksheet = workbook.sheets[' statistical table']
value = worksheet . range(' a 1 ')。 Option (pd. DataFrame,header = 1,index =False,expand ='table ')。 value
Data = value.groupby ('category') # Group data by' category'.
For idx, packet data:
New _ worksheet = workbook.sheets.add (idx) # Add a worksheet to the workbook and name it the current product name.
New worksheet ['a 1']. Option (index = false). Value = group # Adds data to the newly added worksheet.
workbook.save()
workbook.close()
app.quit()
The implementation results are as follows:
Successfully divided into three tables.
_ _ _ _ _ _ _ _ _ _ _ _ End _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _