python 拆分同一个表格中的不同选项
标签搜索

python 拆分同一个表格中的不同选项

irx999
2022-12-09 / 0 评论 / 85 阅读 / 正在检测是否收录...

将同选项不同数据分到不同的表格中
lbgk5e98.png

def Generate():



        
    
    #path="Z:/ME/工作需要用到的工具/一键生成不同的券的表格/"
    path = f"{os.path.split(os.path.realpath(__file__ ))[0]}/"
    files=os.listdir(path)
    wb = xw.books("生成表.xlsm")
    for i ,f in enumerate(files):
        if f.find("力度")>=0 :
            
            os.remove(path+f)
    
    
    
    
    #x = input("请输入本次活动的名称")
    
    x = "活动"
    #A= xw.sheets["板U卡"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]]
    #A=xw.sheets["A卡"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]]
    A=wb.sheets["生成表"].range("A2:AF1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]]

    '''
    A = pd.concat([xw.sheets["主板"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]],
                #xw.sheets["N卡"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]]
                xw.sheets["AMD套装"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]],
                xw.sheets["intel套装"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]],
                xw.sheets["iTX套装"].range("A2:AV1000").options(pd.DataFrame,header = 1 , index = False).value.loc[:,["SKU",x]]
                ,A
        ],ignore_index= True) #合并两个数组的两个方法
    '''

    #A = A.dropna(subset=)
    

    #print(len(A[x].unique().tolist()))

    for  i in range(len((A[x].unique().tolist()))):
        print(A[x].unique().tolist())
        print(A[x].unique().tolist()[i])
        if A[x].unique().tolist()[i] != None and A[x].unique().tolist()[i] !="":
            app = xw.App(visible=True, add_book=False) #表格可显性 false 为 不显示表格,是否添加新表格
            wb = app.books.add()
            wb.sheets["Sheet1"].range("A2").options(header = None,index= False).value =  A.loc[A[x]==f"{A[x].unique().tolist()[i]}"]
            print(A[x].unique().tolist()[i])
            wb.sheets["Sheet1"].range("A1").value="SKU码"
            wb.sheets["Sheet1"].range("B1").value="请输入需要添加的SKU编码,注意编码为文本格式,如左侧案例"
            wb.sheets["sheet1"].range(f"2:{len(A.loc[A[x]==f'{A[x].unique().tolist()[i]}'])+1}").api.NumberFormat ="@"
            #print(len(A.loc[A[x]==f'{A[x].unique().tolist()[i]}']))
            wb.save(f"{os.path.split(os.path.realpath(__file__ ))[0]}/{A[x].unique().tolist()[i]}.xlsx")
        


        #wb.app.kill()
        #app.quit()
        

        print(i)...
0

评论 (0)

取消