xlwings:
xlwings Python is a library that allows some of the characteristics of data analysis in Excel that can be used Python example, an array comprising a support numpy, pandas Series and the DataFrame. And any other Python libraries, we can use the usual method pip or conda to install it. Detailed documentation (https://www.kancloud.cn/gnefnuy/xlwings-docs/1127455)
In xlwings, there are four major types of objects, arranged in order of decreasing level: App (Excel represents one example), Book, Sheet and Range. In addition to these, we will deal with Chart and Shape objects.
An application operation:
1 import:
import xlwings as xw
2 Open excel
= xw.App App (visible = True, add_book = True) #visible is visible. False representation background. add_book whether to create a new workbook
其他操作:
= False app.screen_updating #: screen update, that is the code for the operation excel you can see, real-time updates can be turned off to speed up the script. The default is True. app.pid #App process pid app.books # returns a list of all the workbook open. Python open and manually open interoperability is not to terminate the process, forced to quit. app.quit () case # is not saved, quit the program excel
二 workbooks
1 New wk objects
There are many commands, select a common enough.
wb = app.books.add () # create a new Book wk = xw.Book () wk = xw.books.add ()
2 Open excel file
wb = app.books.open('filepath') wk = xw.Book('filepath') wk = xw.books.open('filepath')
Example 3 excel open or closed unsaved
xw.Book = wk ( 'Book1') wk = xw.books [ 'Book1'] may be used Index #
If the same file is opened in Excel two examples, it will need to fully defined and contains an application instance. You will pass xw.apps.keys()
find your application instance key (PID):
xw.apps [10559] .books [ 'FileName.xlsx ']
See the examples all processes:
xw.apps.keys () # Output List
the kill all instances of processes:
for I in xw.apps.keys ():
I = 'the taskkill / PID' + STR (I) + '-t -f'
the os.system (I)
4. Save
wb.save (path = None) #: Save the workbook, if the specified path, saved in the current working directory.
5. Close
wk.close () # close in the absence of the saved.
Three pairs worksheet operations
wb = app.books.open('im.xlsx')
sheet = wb.sheets[0]
A return operation target
sheet.activate #<bound method Sheet.activate of <Sheet [im.xlsx]Sheet1>>
2 Back sheet specified book
sheet.book
3 Back - a target range, indicating that all cells on the sheet
sheet.cells # <Range [im.xlsx] Sheet1 $ 1:! $ 1048576> may be used sheet.cells [0,0] .value value acquired cell.
4 Gets or sets the name of the Sheet
sheet.name sheet.names return all worksheet-specific names.
5 get all the charts of the collection sheet
sheet.charts
All data and formatting the table 6 emptying.
sheet.clear()
7 know the exact contents of the worksheet, but retain the formatting
sheet.clear_contents()
8 Delete Sheet
sheet.delete()
Returns the index table 9 (the same excel)
sheet.index
10 Create a new Sheet and make it the active worksheet
wb.sheets.add
(name = None, the before = None, the After = None)
# Parameters: name (str, default None) - name of the new worksheet. If None, the default for Excel name.before (Sheet, default None) - An object that specifies the added.after (Sheet, default None) before adding a new worksheet - a worksheet after the specified worksheet object table added.
Adjusting the width of 11 columns, rows, or both the entire worksheet
sheet.autofit (axis = None) parameters: axis (string, default None) - To automatically adjust the line, one of the following: rows or r, to automatically adjust the column, one of the following: columns hc, and to automatically adjust the line column, no argument
Four operating range
1