Openpyxl
—它是一個用於讀寫 Excel 文件的 Python 庫(擴展名為 xlsx / xlsm / xltx / xltm)。 openpyxl 模塊允許 Python 程序讀取和修改 Excel 文件。
例如,用戶可能必須通過數千行並選擇一些有用的信息才能根據某些標准進行小的更改。使用 Openpyxl 模塊,可以非常高效、輕鬆地完成這些任務。
讓我們看看如何使用 Python 創建和編寫 Excel 工作表。
代碼 #1: 打印活動工作表標題的程序
# import openpyxl module
import
openpyxl
# 從 openpyxl 調用 Workbook() 函數
# 創建一個新的空 Workbook 對象
wb
=
openpyxl.Workbook()
# 獲取活動工作表的工作表
# 從活動屬性中獲取。
sheet
=
wb.active
code>
# 有了 Worksheet 對象之後
# 你可以從
# 標題屬性。
sheet_title
=
sheet.title 代碼>
print
(
"活動工作表標題:"
+
sheet_title)
輸出:
活動工作表標題:工作表
代碼#2:標題名稱更改程序
# import openpyxl module
import
openpyxl
# 從 openpyxl 調用 Workbook() 函數
# 創建一個新的空白 Workbook 對象
wb
=
openpyxl.Workbook ()
# 獲取活動工作表的工作表
# 從活動屬性
sheet
=
wb.active
# 可以更改標題的標題
sheet.title
=
"sheet1"
print
(
" 表名改名為:"
+
sheet.title)
輸出:
工作表名稱是重命名為:sheet1
代碼#3: Excel 工作表編寫器
# 導入 openpyxl m odule
import openpyxl
# 從 openpyxl 調用 Workbook() 函數
# 創建一個新的空 Workbook 對象
wb = openpyxl.Workbook()
# 獲取活動工作表的工作表
# 從活動屬性
工作表 = wb.active
# 單元格對像也有行、列
# 和屬性坐標,提供
# 單元格的位置信息。
# 注意:第一行或第一列是整數
# 是1,不是0。cell對像是創建的
#使用sheet對象的cell()方法。
c1 = sheet.cell (row = 1 , column = 1 )
# 將值寫入單元格
c1.value = "ANKIT"
c2 = sheet.cell (row = 1 , 列 = 2 ) c2.value = "RAI"
# 如果你有 Worksheet 對象,你可以
# 訪問單元格對像也以他的名字命名。
# A2 表示 column = 1 & amp; line = 2. c3 = 表格[ `A2` ]
c3. value = "RAHUL"
# B2 表示列 = 2 & amp; line = 2.
c4 = 表格[ `B2` ]
c4. value = "RAI"
# 每次更改 Workbook 對象
# 或其工作表和單元格,電子表格
# 文件在調用本書的
#save() 方法之前不會被保存。
wb.save( "C: Users user Desktop demo.xlsx" )
輸出:
 代碼#4:向工作簿添加工作表的程序 # 導入 openpyxl 模塊
import openpyxl
# 從 openpyxl 調用 Workbook() 函數
# 新建一個空白工作簿對象
wb = openpyxl.工作簿()
sheet = wb.active
# Sheets can be added t o book 使用
#create_sheet() 方法的書本對象。
wb.create_sheet (index = 1 , title = "demo sheet2" )
wb.save ( "C: Users user Desktop demo.xlsx" )
輸出:

Shop
Learn programming in R: courses $
Best Python online courses for 2022 $
Best laptop for Fortnite $
Best laptop for Excel $
Best laptop for Solidworks $
Best laptop for Roblox $
Best computer for crypto mining $
Best laptop for Sims 4 $
Latest questions
NUMPYNUMPY
psycopg2: insert multiple rows with one query
12 answers
NUMPYNUMPY
How to convert Nonetype to int or string?
12 answers
NUMPYNUMPY
How to specify multiple return types using type-hints
12 answers
NUMPYNUMPY
Javascript Error: IPython is not defined in JupyterLab
12 answers
Wiki
Python OpenCV | cv2.putText () method
numpy.arctan2 () in Python
Python | os.path.realpath () method
Python OpenCV | cv2.circle () method
Python OpenCV cv2.cvtColor () method
Python - Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
Python os.path.join () method
|