When you create a workbook by openpyxl module, without a local pre-created excel, it creates a new excel files directly
When you create a workbook, a worksheet will contain at least
Example of use:
Import module Workbook class openpyxl
from openpyxl import Workbook
Create a workbook, workbook and get the first sheet
Workbook = wb () # create a Workbook WS = wb.active # get the current first worksheet workbook, the default index value is 0, it can be changed
Create a worksheet
= wb.create_sheet ws1 () # append a new worksheet at the end of the current workbook, the name is automatically assigned, such as Sheet1, Sheet2, .... ws2 = wb.create_sheet (0) # specified index in the current workbook at append a new worksheet, the name is automatically assigned, such as Sheet1, Sheet2, .... ws1.title = " new Title1 " # specify the sheet name Title1 new ws2.title = " new Title2 " # specify the sheet name Title2 new ws3 = wb.create_sheet ( " MyNewSheet1 " ) # append at the end of the current workbook in a new worksheet, and specify the sheet name MyNewSheet1 WS4 = wb.create_sheet ( " MyNewSheet2 " , 0) #Append the specified index in the current workbook in a new worksheet, and specify the sheet name MyNewSheet2
Gets worksheet by worksheet name
WB = ws5 [ " MyNewSheet2 " ] Print (wb.sheetnames) # outputs the worksheet all workbook in a list form for Sheet in WB: Print (sheet.title) # name of each worksheet in the workbook cycle output
Set the color of the worksheet
= ws.sheet_properties.tabColor " 1072BA " # set the color of the worksheet, the color code set value RRGGBB
Copy of the current worksheet
source = wb.active
target = wb.copy_worksheet(source)
note:
1) does not support a copy of the worksheet from a workbook to another workbook
2) If the current workbook is read-only or write-only mode, the copy does not support the worksheet
3) Copy the worksheet, the worksheet not all content will is copied, the copy can be specifically as follows:
a) where the value of the Cell, styles, links and notes
b) certain attributes worksheet, such as the dimension, form and property
c) workbook and a worksheet or more other attributes are not copied, such as pictures, graphics, etc.
Save the current settings to a local file
wb.save ( " sample.xlsx " ) # will eventually export the results to a local file