pythonxlwings使⽤说明_详解python的xlwings库读写excel操
作总结
⼀、总结(点击显⽰或隐藏总结内容)
⼀句话总结:
xlwings 是 Python 中操作Excel的⼀个第三⽅库,⽀持.xls读写,.xlsx读写,操作⾮常简单,功能也很强⼤
1、xlwings 中的逻辑:应⽤->⼯作簿->⼯作表->范围 对应的代码?
应⽤:⼀个应⽤(⼀个xlwings程序):app = xw.App(visible=True, add_book=False)
⼯作簿(book):excel⽂件(excel程序):wb = app.books.add()
⼯作表(sheet):sheet:sht = wb.sheets['sheet1']
范围:⾏列:sht.range('a6').expand('table').value = [['a','b'],['d','e']]
import xlwings as xw
# 写到Excel中去
# add_book也就是是否增加excel 的book
# visible=True 表⽰操作过程是否可显⽰
app = xw.App(visible=True, add_book=False)
# ⼯作簿
wb = app.books.add()
# 页sheet1
sht = wb.sheets['sheet1']
# 单个值插⼊
# sht.range('A1').value = '产品名称'
# sht.range('B1').value = '编号'
# sht.range('C1').value = '价格'
# sht.range('A2').value = '不告诉你'
# sht.range('B2').value = 'n110110'
# sht.range('C2').value = '688.26'
# sht.range('A3').value = '不告诉你1'
# sht.range('B3').value = 'n1101101'
# sht.range('C3').value = '688.261'
# 插⼊⼀⾏
# sht.range('a1').value = [1,2,3,4]
# 等同于
# sht.range('a1:d4').value = [1,2,3,4]
# 插⼊⼀列
# sht.range('a2').options(transpose=True).value = [5,6,7,8]
# 同时插⼊⾏列
# sht.range('a6').expand('table').value = [['a','b','c'],['d','e','f'],['g','h','i']]
# 在当前⽬录下⽣成⽂件
wb.save('demo1.xlsx')
wb.close()
app.quit()
# import os
# path1=os.path.abspath('.') # 表⽰当前所处的⽂件夹的绝对路径
# print(path1)
# path2=os.path.abspath('..') # 表⽰当前所处的⽂件夹上⼀级⽂件夹的绝对路径
# print(path2)
# 关于路径问题,切换到指定⽬录即可
2、xlwings库向excel 插⼊值的⽅式?
a、插⼊单个值:sht.range('A1').value = '产品名称'
b、插⼊⼀⾏:sht.range('a1').value = [1,2,3,4] 或者 sht.range('a1:d4').value = [1,2,3,4]
c、插⼊⼀列:sht.range('a2').options(transpose=True).value = [5,6,7,8]
d、同时插⼊⾏列:sht.range('a6').expand('table').value = [['a','b','c'],['d','e','f'],['g','h','i']]
3、xlwings库 读写excel基本操作步骤?
就是按照xlwings库逻辑(应⽤->⼯作簿->⼯作表->范围)操作即可,打开的记得关就好了,该读就读,该写就写app = xw.App(visible=True, add_book=False)
# ⼯作簿
wb = app.books.add()
# 页sheet1
sht = wb.sheets['sheet1']
python怎么读入excel# 同时插⼊⾏列
sht.range('a6').expand('table').value = [['a','b','c'],['d','e','f'],['g','h','i']]
# 在当前⽬录下⽣成⽂件
wb.save('demo1.xlsx')
wb.close()
app.quit()
4、xlwings库 读取excel中的数据的⽅式?
print(sht.range('a1:c7').value),这样读⾏,读列,还是同时读⾏读列都可以
# 读取⾏列:读取A1:C7(直接填⼊单元格范围就⾏了),得到⼀个⼆维列表print(sht.range('a1:c7').value)
# 读取⾏:得⼀维列表
# print(sht.range('a1:c1').value)
# 读取列:得⼀维列表
# print(sht.range('a1:a7').value)
import xlwings as xw
app = xw.App(visible=True, add_book=False)
# 显⽰警报()
app.display_alerts = True
# 屏幕更新(应⽤中)
app.screen_updating = True
# 打开⽂件
wb = app.books.open('demo1.xlsx')
sht = wb.sheets['sheet1']
# 遍历读取单元格
# column_name = ['A','B',"C"]
# data_list = [] #将数据存到list中去
# for i in range(3): # 遍历⾏
# row_list = []
# for j in range(3): #遍历列
# str1 = column_name[j]+str(i+1)
# a = sht.range(str1).value
# row_list.append(a)
# print(a)
# pass
# data_list.append(row_list)
# pass
# print(data_list)
# 读取⾏列:读取A1:C7(直接填⼊单元格范围就⾏了),得到⼀个⼆维列表print(sht.range('a1:c7').value)
# 读取⾏:得⼀维列表
# print(sht.range('a1:c1').value)
# 读取列:得⼀维列表
# print(sht.range('a1:a7').value)
wb.save()
wb.close()
app.quit()
⼆、python的xlwings库读写excel操作总结
1、写
"""
xlwings介绍
xlwings 是 Python 中操作Excel 的⼀个第三⽅库,
⽀持.xls读写,.xlsx读写
操作⾮常简单,功能也很强⼤
1、安装库
pip3 install xlwings
2、引⼊库
import xlwings as xw
3、
应⽤->⼯作簿->⼯作表->范围
应⽤:⼀个应⽤(⼀个xlwings程序):
app = xw.App(visible=True, add_book=False)
⼯作簿(book):
excel⽂件(excel程序):wb = app.books.add()
⼯作表(sheet):
sheet:sht = wb.sheets['sheet1']
范围:⾏列:
sht.range('a6').expand('table').value = [['a','b'],['d','e']]
xlwings.App(visible=True,add_book=False)
其中参数visible(表⽰处理过程是否可视,也就是处理Excel的过程会不会显⽰出来),add_book(是否打开新的Excel程序,也就是是不是打开⼀个新的excel窗⼝)
"""
import xlwings as xw
# 写到Excel中去
# add_book也就是是否增加excel 的book
# visible=True 表⽰操作过程是否可显⽰
app = xw.App(visible=True, add_book=False)
# ⼯作簿
wb = app.books.add()
# 页sheet1
sht = wb.sheets['sheet1']
# 单个值插⼊
# sht.range('A1').value = '产品名称'
# sht.range('B1').value = '编号'
# sht.range('C1').value = '价格'
# sht.range('A2').value = '不告诉你'
# sht.range('B2').value = 'n110110'
# sht.range('C2').value = '688.26'
# sht.range('A3').value = '不告诉你1'
# sht.range('B3').value = 'n1101101'
# sht.range('C3').value = '688.261'
# 插⼊⼀⾏
# sht.range('a1').value = [1,2,3,4]
# 等同于
# sht.range('a1:d4').value = [1,2,3,4]
# 插⼊⼀列
# sht.range('a2').options(transpose=True).value = [5,6,7,8]
# 同时插⼊⾏列
# sht.range('a6').expand('table').value = [['a','b','c'],['d','e','f'],['g','h','i']]
# 在当前⽬录下⽣成⽂件
wb.save('demo1.xlsx')
wb.close()
app.quit()
# import os
# path1=os.path.abspath('.') # 表⽰当前所处的⽂件夹的绝对路径
# print(path1)
# path2=os.path.abspath('..') # 表⽰当前所处的⽂件夹上⼀级⽂件夹的绝对路径# print(path2)
# 关于路径问题,切换到指定⽬录即可
2、读
import xlwings as xw

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。