使用python修改excel电子表格

使用python修改excel电子表格

前言

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

1. python相关代码

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

import numpy
import xlwings

def rand_numbers():
    wb = xlwings.Book.caller()
    table = wb.sheets[0].range('A1').expand().value
    districts = 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_content
        nb.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实现帯格式的拷贝。

猜你喜欢

转载自blog.csdn.net/harborian/article/details/89032383