Fork me on GitHub

Python xlwings 更新表格sheet

需求

有A、B、C三个表格,需要根据A的sheet(名字为“定义”)来更新B、C对应的sheet,并且要保持表格的样式不变。

实现

# #####################################################
# Function: update Excel information and remain the style
# Install:
# a.install it manually with pip:
# pip install xlwings
# b.install with offline file tar.gz with pip:
# pip install xlwings-0.24.9.tar.gz
# Reference: https://github.com/xlwings/xlwings
# #####################################################
import xlwings as xw
import os
def updateInfo(sheetName, path, save_path):
# file FROM you want to copy sheet
wb = xw.Book(path)
# select sheet you want to copy
sht = wb.sheets[sheetName]
# file where you want to copy
new_wb = xw.Book(save_path)
print(new_wb.sheets)
# copy needed sheet to the new_wb
# set the filed from cell A1 to cell Z240
sht.range('A1:Z240').api.Copy(new_wb.sheets[sheetName].range('a1').api)
new_wb.save(save_path)
if __name__ == "__main__":
sheetName = "定义"
baseFile = "\A.xlsx"
updateFile_1 = r"\B.xlsx"
updateFile_2 = r"\C.xlsx"
# print(os.getcwd())
path = os.getcwd() + baseFile
save_path1 = os.getcwd() + updateFile_1
save_path2 = os.getcwd() + updateFile_2
# update info
updateInfo(sheetName, path, save_path1)
updateInfo(sheetName, path, save_path2)

优化

# #####################################################
# Function: update Excel information and remain the style
# Install:
# a.install it manually with pip:
# pip install xlwings
# b.install with offline file tar.gz with pip:
# pip install xlwings-0.24.9.tar.gz
# Reference: https://github.com/xlwings/xlwings
# #####################################################
import xlwings as xw
import os
def updateInfo(sheetName, path, save_path):
# set app invisible
app = xw.App(visible=False, add_book=False)
app.display_alerts = False
app.screen_updating = False
# file FROM you want to copy sheet
# wb = xw.Book(path)
wb = app.books.open(path)
# select sheet you want to copy
sht = wb.sheets[sheetName]
# file where you want to copy
# new_wb = xw.Book(save_path)
new_wb = app.books.open(save_path)
# print(new_wb.sheets)
# copy needed sheet to the new_wb
# set the filed from cell A1 to cell Z240
sht.range('A1:Z240').api.Copy(new_wb.sheets[sheetName].range('a1').api)
new_wb.save(save_path)
wb.close()
new_wb.close()
if __name__ == "__main__":
sheetName = "定义"
baseFile = "\A.xlsx"
updateFile_1 = r"\B.xlsx"
updateFile_2 = r"\C.xlsx"
# print(os.getcwd())
path = os.getcwd() + baseFile
save_path1 = os.getcwd() + updateFile_1
save_path2 = os.getcwd() + updateFile_2
# update info
print("start update information")
updateInfo(sheetName, path, save_path1)
updateInfo(sheetName, path, save_path2)
print("update information finish")

参考资料

xlwings

xlwings操控excel表格

posted @   ZTianming  阅读(323)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
欢迎阅读『Python xlwings 更新表格sheet』

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示