【笔记】创建excel表并保存指定路径

文件夹创建

excel表创建及保存

import xlwt
import os
def mikdir(path):
    path = path.strip()
    # path = path.rstrip("\\")
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print('创建成功')
    else:
        print("文件夹已存在")
    return


def savedata(path):
    print("saving")
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('20211117练习', cell_overwrite_ok=True)
    col = ("1111", '2222','333','44')
    print('1')
    for i in range(0,4):
        sheet.write(0,i,col[i])
    for i in range(0,10):
        for j in range(0,4):
            sheet.write(i+1, j, i*j)
    print('2')
    book.save(path)


def main():
    dir_path = 'E:/测试'   #  文件夹路径
    file_name = 'aaaa.xls'   #  文件名
    # mikdir(dir_path)    #
    path = os.path.join(dir_path,file_name)
    # savedata(os.path.join(dir_path,file_name))


    print(os.path.basename(path))   #  文件名
    print(os.path.dirname(path))   #目录名


    path = "F:\\"+"0000000\\"+'\\000000000.xls'
    # path = "F:\\" + "0000000\\"
    # mikdir(path)
    # savedata(path)
main()

Python os.path() 模块 | 菜鸟教程 (runoob.com)

关于路径操作

上一篇:[Leetcode][Python]23: Merge k Sorted Lists


下一篇:Cat.1应用 | 智能音箱抢市场,你需要一个好方案