100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 使用python修改excel电子表格

使用python修改excel电子表格

时间:2024-01-26 00:21:34

相关推荐

使用python修改excel电子表格

使用python修改excel电子表格

前言1. python相关代码2. 问题

前言

需要根据第一个excel表格的第一类,对excel表进行拆分。之前,都是使用vba。对于excel操作,如果可以使用python的话,可以提高效率。xlwings可以提供对电子表格的操作,因此从网上下载了xlwings。

1. python相关代码

根据sheet1的第一类,拆分表格成多个文件。

import numpyimport xlwingsdef rand_numbers():wb = xlwings.Book.caller()table = wb.sheets[0].range('A1').expand().valuedistricts = list(set(wb.sheets[0].range('A2').expand('down').value))for dist in districts:dist_content = []dist_content.append(table[0])for row in table:if row[0]==dist:print(row)dist_content.append(row)nb = xlwings.Book()nb.sheets[0].range('A1').expand().value = dist_contentnb.save(dist + ".xls")nb.close()if __name__ == '__main__':xlwings.Book('excel path file').set_mock_caller()copy_with_format() xlwings.Book.caller().close()

2. 问题

Range的value属性确实方便,但是转换后,格式和公式都不存在了。如果使用format和formula,也非常的不方便。

不知道怎么使用python实现帯格式的拷贝。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。