python读文件,写文件,数组,遍历目录

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import xlrd
import xlwt
from xlwt import Workbook
import os
import os.path
from openpyxl.reader.excel import load_workbook
from xlutils.copy import copy

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

# 遍历目录里所有的excel文件,并修改每一个文件特定的单元格
def modifycell():
    rootdir = 'C:\\Users\\Administrator\\Documents\\七色米模版\\标准钢构\\3p3k'
    list = os.listdir(rootdir)
    for i in range(0, len(list)):
        path = os.path.join(rootdir, list[i])
        print(path)
        rb = xlrd.open_workbook(path)
        wb = copy(rb)
        ws = wb.get_sheet(0)
        # for i in range(3, 28):
        #     for j in range(0, 3,2):
        #
        #         print(i, j )
                # ws.write(i, j, label='')
        # wb.save(path)

def read_dc3k_cell():
    readfilename = 'C:\\Users\\Administrator\\Documents\\七色米模版\\bb钢架.xls'
    workbook1 = xlrd.open_workbook(readfilename)
    sheet1 = workbook1.sheet_by_name("单层3k材料")
    wfn=int(sheet1.cell(6, 4).value)+1
    # print(wfn)
    datalist=[]
    for i in range(2, 28):
        data = []
        for j in range(0, 5, 2):
            data.append(sheet1.cell(i, j).value)
            # print(sheet1.cell(i, j).value)
        datalist.append(data)
    # for i in range(len(datalist)):
    #     for j in range(len(datalist[i])):
    #         print(datalist[i][j])
    datalist.append(wfn)
    return datalist

def read_and_modify_cell():
    # 取得要读取的文件的信息
    readfilename = 'C:\\Users\\Administrator\\Documents\\七色米模版\\bb钢架.xls'
    workbook1 = xlrd.open_workbook(readfilename)
    sheet1 = workbook1.sheet_by_name("双层4k材料")
    wfn = int(sheet1.cell(6, 4).value) + 1
    # print(wfn)
    # 要写入的文件,找到表
    rootdir = 'C:\\Users\\Administrator\\Documents\\七色米模版\\标准钢构\\6p4k\\6p4k'
    path=rootdir+str(wfn)+'k.xls'
    print(path)
    rb = xlrd.open_workbook(path)
    wb = copy(rb)
    ws = wb.get_sheet(0)
    for i in range(2, 45):
        for j in range(0, 5, 2):
            # print(str(sheet1.cell(i, j).value))
            ws.write(i, j, label=sheet1.cell(i, j).value)
    wb.save(path)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    read_and_modify_cell()
    # read_dc3k_cell()

    # 修改xls文件里的单元格
    # rb = xlrd.open_workbook('c:\\1.xls')  # 打开weng.xls文件
    # wb = copy(rb)  # 利用xlutils.copy下的copy函数复制
    # ws = wb.get_sheet(0)  # 获取表单0
    # ws.write(30, 0, label='好的')  # 增加(8,0)的值
    # wb.save('c:\\1.xls')

    # 读取excel文件里的单元格
    # workbook1=xlrd.open_workbook("c:\\1.xls")
    # sheet1=workbook1.sheet_by_name("Sheet1")
    # print(sheet1.cell(0,1).value)



# See PyCharm help at https://www.jetbrains.com/help/pycharm/

你可能感兴趣的:(python,python)