python excel docx msoffcrypto pandas

#coding=utf-8


# import tkinter as tk
# from tkinter import *
import os

import win32com.client as win32
import xlwt
from datetime import date

from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
import re
import tempfile

import xlrd
from xlutils.copy import copy
# import msoffcrypto
#解决warning: UserWarning: Workbook contains no default style, apply openpyxl's default
import warnings
warnings.simplefilter('ignore')

import pandas as pd  # 读取Excel并进行数据分析
import msoffcrypto   # 读取加密的Excel文件
import io            # 将文件写入到内存中
# 定义一个函数,用来导入Excel文件,并将获取的dataFrame数据结构返回
def read_excel(path, sheet_name, title, header=0, password=''):
    decrypted = io.BytesIO()   # 用来后来将读取到的Excel文件解密之后写入内存中。
    try:
        with open(path, 'rb') as f:   # 用上下文将文件打开
            # 判断Excel文件是否加密,未加密直接pandas读取,加密的就先解密到内存中然后pandas读取。
            if not msoffcrypto.olefile.isOleFile(f):   # mscoffcrypto有可以判断文件是否加密的功能。
                pd_excel = pd.read_excel(f, sheet_name, header=header)
                logging.info("{title}文件读取成功".format(title=title))
            else:
                xlf = msoffcrypto.OfficeFile(f)   # 用msoffcrypto打开加密文件,未加密文件用这个打开会报错
                xlf.load_key(password=password)   # 传入文件的密码
                xlf.decrypt(decrypted)            # 将文件解密并保存到内存中,定义descrypted为io.BytesIO() ,这是msoffcrypto常用方法。
                print('xlf.decrypt(decrypted)',decrypted)
                pd_excel = pd.read_excel(decrypted, names=['a','b','c','d','e','f','g','h','i'])  # 将保存到内存中的文件用pandas打开。
                # print(pd_excel)
                return pd_excel
                # pd_excel = pd.read_excel(decrypted, sheet_name, header=header)  # 将保存到内存中的文件用pandas打开。
                # 通过pandas读取Excel的内容,指定文件路径名称以及要读取的sheet的名称
                # logging.info("{title}文件读取成功".format(title=title))
    except Exception as e:   # 如果文件打开失败,则将报错信息记录到日志中,并向管理员发送邮件。
        error_msg = "Excel文件{title}打开失败,请检查文件路径及权限。".format(title=title)
        # logging.critical(error_msg)
        # logging.critical(e)
        # send_waring_mail(error_msg, title)
        return
    # pd.set_option("display.max_columns", None)   # 将pandas输出设置为显示所有列。在辅助print打印时使用
    # 将责任时间的数据格式转换成datetime类型,如此在后期可以参与实践比较
    # pd_excel['责任时间'] = pd.to_datetime(pd_excel['责任时间'])
    print(pd_excel)
    return pd_excel


def open_workbook(workbook_file_path, password=None):
    if password is None:
        wb = xlrd.open_workbook(workbook_file_path)
    else:
        with open(workbook_file_path, 'rb') as fin,\
                tempfile.TemporaryFile() as tfp:
            encrypted = msoffcrypto.OfficeFile(fin)
            encrypted.load_key(password=password)
            encrypted.decrypt(tfp)
            tfp.seek(0)
            # wb = xlrd.open_workbook(file_content=tfp.read())
            wb = xlrd.open_workbook( tfp)
    return wb

a=str(date.today())

#定义大窗口。
# window =tk.Tk()
# window.title('自动统计') # 标题
# window.geometry('300x300') # 窗口尺寸


# def shurupassword():
#     global getpassword
#     getpassword=entry1.get()
    
# bt1 = tk.Button(window, text='输入密码', width=15, height=2, command=shurupassword)
# bt1.pack()

# entry1=tk.Entry(window,width=50,bg="green",fg="black")
# entry1.pack()

# 引入 datetime 模块
import datetime
today = datetime.datetime.today()
year = today.year
month = today.month
day = today.day

def getYesterday(): 
    today=datetime.date.today() 
    oneday=datetime.timedelta(days=1) 
    yesterday=today-oneday  
    return yesterday
 
# 输出
print(getYesterday(),year,month,day)
yesday = getYesterday()
yyear = yesday.year
ymonth = yesday.month
yday = yesday.day

def replaceinrun(replacedstr,replacestr, arun):
    if replacedstr in arun.text:
        arun.text=arun.text.replace(replacedstr,replacestr)
        print(arun.text)

def reparagraph(replacedstr,replacestr, aparagraph):
    for run in aparagraph.runs:
        replaceinrun(replacedstr, replacestr, run)


def han():
    document=Document(r"函模板.docx")
    paragraphs=document.paragraphs
    i = 0
    for pa in paragraphs:
        print(i, pa.text)
        i = i+1
    print(paragraphs[2].text)
    # text=re.sub('11',str(ymonth),paragraphs[2].text)
    # text=re.sub('21',str(yday),text)
    # paragraphs[2].text=text

    for run in paragraphs[2].runs:
        replaceinrun('ym', str(ymonth), run)
        replaceinrun('yd', str(yday), run)
    for run in paragraphs[5].runs:
        replaceinrun('ym', str(ymonth), run)
        replaceinrun('yd', str(yday), run)
    for run in paragraphs[7].runs:
        replaceinrun('ym', str(ymonth), run)
        replaceinrun('yd', str(yday), run)
        replaceinrun('yyyy', str(yyear), run)
    for run in paragraphs[13].runs:
        replaceinrun('mm', str(month), run)
        replaceinrun('dd', str(day), run)
        replaceinrun('yyyy', str(year), run)
        # if "21" in run.text:
        #     run.text=run.text.replace('21',str(yday))
        #     print(run.text)

    document.save('关于提供'+str(ymonth)+'月'+str(yday)+'日广州市手机用户漫游情况的函.docx')

# han()
print(getYesterday(),year,month,day)

def changecell(cell, atext):
                for paragraph in cell.paragraphs:
                        for run in paragraph.runs:
                            run.text = str(atext)
def getfilename(keyw):
    file_dir=os.getcwd ()
    # xlAPP=win32.Dispatch("Excel.Application")
    # xls = win32com.client.DispatchEx('Excel.Application')
    # xlAPP.Visible=False
    for root,dirs,files in os.walk(file_dir):
        for name in files:
            if (name.find(keyw)>-1) and (name.find('xls')>-1):
                print(name)
                filename=os.path.join(root,name)
                print(filename)
                return filename

def getdatenum(filename):
    print(filename)
    p202=filename.find('202')
    strdate = filename[p202:len(filename)-5]
    print(strdate)
    return strdate

def getdate(filename):
    print(filename)
    p202=filename.find('202')
    strdate = filename[p202:len(filename)-5]
    print(strdate)
    a = strdate.split('-')
    return a[1]+'月'+a[2]+'日'

def savefile(pd, filename):
    print(filename)
    p202=filename.find('202')
    strdate = filename[p202:len(filename)-5]
    print(strdate)
    pn=filename.rfind('\\')+1
    savename = filename[0:p202]
    print(savename)
    pd['date']=strdate
    print(pd)
    pd.to_csv(savename+'.csv', mode='a')

def fujian():
    document=Document(r"附件模板.docx")
    paragraphs=document.paragraphs
    i = 0
    for pa in paragraphs:
        print(i, pa.text)
        i = i+1
    for run in paragraphs[3].runs:
        replaceinrun('mm', str(month), run)
        replaceinrun('dd', str(day), run)
        replaceinrun('yyyy', str(year), run)
    for run in paragraphs[6].runs:
        replaceinrun('lm', str(ymonth), run)
        replaceinrun('ld', str(yday), run)
    file_dir=os.getcwd ()
    filenameout = ''
    filenamein = ''

    # xlAPP=win32.Dispatch("Excel.Application")
    # xls = win32com.client.DispatchEx('Excel.Application')
    # xlAPP.Visible=False
    for root,dirs,files in os.walk(file_dir):
        for name in files:
            if name.find('漫出')>-1:
                print(name)
                filename=os.path.join(root,name)
                filename = getfilename('漫出')
                print(filename)
                
                filenameout = filename
                wb = read_excel(filename, '','',0,'passwrod')
                # wb = open_workbook(filename, 'passwrod')
                # table = wb.sheets()[0]
                # print(table.cell(1,1))
                print(wb)
                pdout=wb
                print(wb[['e','f','h','i']])
                province = wb[wb['f'].isna()]
                print(province)
                print(province['h'].sum())
                sumall = province['h'].sum()/10000
                gdoutall = wb.iloc[[0],[7]].values[0][0]/10000
                reparagraph('2.8', '{:.2f}'.format(sumall-gdoutall), paragraphs[6])
                
                reparagraph('17.5', '{:.2f}'.format(province['h'].sum()/10000), paragraphs[6])
                reparagraph('14.7', '{:.2f}'.format(gdoutall), paragraphs[6])
                print(province['i'].sum())
                c5 = wb.iloc[[1],[5]].values[0][0]+'、'+wb.iloc[[2],[5]].values[0][0]+'、'+wb.iloc[[3],[5]].values[0][0]+'、'+wb.iloc[[4],[5]].values[0][0]+'、'+wb.iloc[[5],[5]].values[0][0]
                reparagraph('c', c5, paragraphs[6])
                s5 = province.iloc[[1],[4]].values[0][0]+'、'+province.iloc[[2],[4]].values[0][0]+'、'+province.iloc[[3],[4]].values[0][0]+'、'+province.iloc[[4],[4]].values[0][0]+'、'+province.iloc[[5],[4]].values[0][0]
                reparagraph('s', s5, paragraphs[6])
                print(province.iloc[[2],[4]].values[0][0], pd.notnull(province.iloc[[2],[4]]), pd.notnull(province.iloc[[2],[4]]).sum())
                print(province.iloc[[2],[5]].values[0][0], pd.notnull(province.iloc[[2],[5]]), pd.notnull(province.iloc[[2],[5]]).sum())

                print(province.iloc[[2],[4]].values[0][0])
                print(wb.iloc[[2],[5]].values[0][0])
                print(wb.iloc[[2],[7]].values[0][0])
                print(wb.iloc[[2],[8]].values[0][0])
                # 修改表格里面的内容
                tables=document.tables
                changecell(tables[0].cell(1,2), int(wb.iloc[[2],[7]].values[0][0]))
                changecell(tables[0].cell(1,3), '{:.2%}'.format(wb.iloc[[2],[8]].values[0][0]))
                for i in range(0,25):
                    print(pd.notnull(wb.iloc[[i],[5]]).sum().sum())
                    if pd.notnull(wb.iloc[[i],[5]]).sum().sum()>0:

                        print(i, wb.iloc[[i],[5]].values[0][0])
                i = 1
                while pd.notnull(wb.iloc[[i],[5]]).sum().sum()>0:
                    changecell(tables[0].cell(i,1), str(wb.iloc[[i],[5]].values[0][0]))
                    changecell(tables[0].cell(i,2), int(wb.iloc[[i],[7]].values[0][0]))
                    changecell(tables[0].cell(i,3), '{:.2%}'.format(wb.iloc[[i],[8]].values[0][0]))
                    i +=1

                
                print(i, wb.iloc[[i],[4]].values[0][0])
                if i<21:
                    for j in (i, 21):
                        changecell(tables[0].cell(j,1), '')
                        changecell(tables[0].cell(j,2), '')
                        changecell(tables[0].cell(j,3), '')
                        print(j, wb.iloc[[j],[4]].values[0][0])
                
                print(province.shape[0])
                prow = province.shape[0]
                for i in range(1,31):
                    if (i)< prow:
                        changecell(tables[1].cell(i,1), str(province.iloc[[i],[4]].values[0][0]))
                        changecell(tables[1].cell(i,2), int(province.iloc[[i],[7]].values[0][0]))
                        changecell(tables[1].cell(i,3), '{:.2%}'.format(province.iloc[[i],[8]].values[0][0]))
                    else:
                        changecell(tables[1].cell(i,1), '')
                        changecell(tables[1].cell(i,2), '')
                        changecell(tables[1].cell(i,3), '')



                # for paragraph in tables[0].cell(1,1).paragraphs:
                #         for run in paragraph.runs:
                #             run.text = str(wb.iloc[[2],[5]].values[0][0])

# ————————————————
# 版权声明:本文为CSDN博主「soni321」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
# 原文链接:https://blog.csdn.net/soni321/article/details/124083536
                # tables[0].cell(1,1).text=wb.iloc[[2],[5]].values[0][0]



                # xls=xlAPP.Workbooks.Open(filename,False,True,None,Password='passwrod')
                # sheet=xls.Worksheets(1)
                # wr = sheet.Cells(1,1)
            # filename=os.path.join(root,name)

    for run in paragraphs[14].runs:
        replaceinrun('m', str(ymonth), run)
        replaceinrun('d', str(yday), run)
    filename = getfilename('漫入')
    print(filename)
    filenamein = filename
    wb = read_excel(filename, '','',0,'passwrod')
    print(wb)
    pdin = wb
    print(wb[['b','c','h','i']])
    province = wb[wb['c'].isna()]
    print(province)
    print(province['h'].sum())
    sumall = province['h'].sum()/10000
    gdoutall = wb.iloc[[0],[7]].values[0][0]/10000
    reparagraph('7.1', '{:.2f}'.format(sumall-gdoutall), paragraphs[14])
    
    reparagraph('17.5', '{:.2f}'.format(province['h'].sum()/10000), paragraphs[14])
    reparagraph('10.5', '{:.2f}'.format(gdoutall), paragraphs[14])
    print(province['i'].sum())
    print(wb.iloc[[1],[2]].values[0][0], wb.iloc[[1],[3]].values[0][0])
    c5 = wb.iloc[[1],[2]].values[0][0]+'、'+wb.iloc[[2],[2]].values[0][0]+'、'+wb.iloc[[3],[2]].values[0][0]+'、'+wb.iloc[[4],[2]].values[0][0]+'、'+wb.iloc[[5],[2]].values[0][0]
    reparagraph('c', c5, paragraphs[14])
    s5 = province.iloc[[1],[1]].values[0][0]+'、'+province.iloc[[2],[1]].values[0][0]+'、'+province.iloc[[3],[1]].values[0][0]+'、'+province.iloc[[4],[1]].values[0][0]+'、'+province.iloc[[5],[1]].values[0][0]
    reparagraph('s', s5, paragraphs[14])
    i = 1
    while pd.notnull(wb.iloc[[i],[2]]).sum().sum()>0:
        changecell(tables[2].cell(i,1), str(wb.iloc[[i],[2]].values[0][0]))
        changecell(tables[2].cell(i,2), int(wb.iloc[[i],[7]].values[0][0]))
        changecell(tables[2].cell(i,3), '{:.2%}'.format(wb.iloc[[i],[8]].values[0][0]))
        i +=1
    if i<21:
        for j in (i, 21):
            changecell(tables[2].cell(j,1), '')
            changecell(tables[2].cell(j,2), '')
            changecell(tables[2].cell(j,3), '')
            print(j, wb.iloc[[j],[1]].values[0][0])
    prow = province.shape[0]
    for i in range(1,31):
        if (i)< prow:
            changecell(tables[3].cell(i,1), str(province.iloc[[i],[1]].values[0][0]))
            changecell(tables[3].cell(i,2), int(province.iloc[[i],[7]].values[0][0]))
            changecell(tables[3].cell(i,3), '{:.2%}'.format(province.iloc[[i],[8]].values[0][0]))
        else:
            changecell(tables[3].cell(i,1), '')
            changecell(tables[3].cell(i,2), '')
            changecell(tables[3].cell(i,3), '')

    document.save('附件1:关于'+str(yyear)+'年'+str(ymonth)+'月'+str(yday)+'日广州市手机用户漫游情况的通报.docx')
    savefile(pdin,filenamein)
    savefile(pdout,filenameout)
    # xls.Close()

# fujian()

def gdru():
    filename = getfilename('广东省漫入省份排行')
    print(filename)
    wb = read_excel(filename, '','',0,'passwrod')
    wb.rename(columns={'f':'漫入地市'},inplace=True)
    print(wb)
    wbp = wb.pivot_table(index='漫入地市',columns='b',values='h', aggfunc='sum')
    wbp['总计'] = wbp.apply(lambda x: x.sum(), axis=1) # 按行求和,添加为新列
    wbp.loc['总计'] = wbp.apply(lambda x: x.sum()) # 各列求和,添加新的行
    print(wbp)
    savefile(wb,filename)
    p202=filename.find('202')
    strdate = filename[p202:len(filename)-5]
    print(strdate)
    pn=filename.rfind('\\')+1
    savename = filename[0:p202]
    print(savename)
    wbp.to_excel('广东省漫入'+strdate+'.xlsx')

# gdru()
import openpyxl
from copy import copy

def copycell(source_cell, target_cell):
    target_cell.value=source_cell.value
    # copy all format and style form src cell
    target_cell.data_type = source_cell.data_type
    target_cell.fill = copy(source_cell.fill)
    target_cell.border = copy(source_cell.border)
    target_cell.font = copy(source_cell.font)
    target_cell.fill = copy(source_cell.fill)
    target_cell.number_format = copy(source_cell.number_format)
    target_cell.protection = copy(source_cell.protection)
    target_cell.alignment = copy(source_cell.alignment)
    # target_cell.data_type = source_cell.data_type
    # target_cell.fill = copy(source_cell.fill)
    # if source_cell.has_style:
    #     print(source_cell._style)
    #     target_cell._style = copy(source_cell._style)
    #     target_cell.font = copy(source_cell.font)
    #     target_cell.border = copy(source_cell.border)
    #     target_cell.fill = copy(source_cell.fill)
    #     target_cell.number_format = copy(source_cell.number_format)
    #     target_cell.protection = copy(source_cell.protection)
    #     target_cell.alignment = copy(source_cell.alignment)
    
    # if source_cell.hyperlink:
    #     target_cell._hyperlink = copy(source_cell.hyperlink)
    
    # if source_cell.comment:
    #     target_cell.comment = copy(source_cell.comment)

def bjsh(table, datestr, rout,rin):
    nrows = table.max_row # 获得行数
    # ncolumns = table.max_column # 获得行数
    # table.cell(row=nrows+1 , column=1)= copy(table.cell(row=nrows , column=1))
    copycell(table.cell(row=nrows , column=1), table.cell(row=nrows+1 , column=1))
    copycell(table.cell(row=nrows , column=2), table.cell(row=nrows+1 , column=2))
    copycell(table.cell(row=nrows , column=3), table.cell(row=nrows+1 , column=3))
    table.cell(row=nrows+1 , column=1).value = datestr
    table.cell(row=nrows+1 , column=2).value = rout
    table.cell(row=nrows+1 , column=3).value = rin

def gz(table, datestr, rout,rin):
    nrows = table.max_row # 获得行数
    print(datestr, rout,rin,nrows)
    # ncolumns = table.max_column # 获得行数
    # table.cell(row=nrows+1 , column=1)= copy(table.cell(row=nrows , column=1))
    copycell(table.cell(row=nrows , column=1), table.cell(row=nrows+1 , column=1))
    copycell(table.cell(row=nrows , column=2), table.cell(row=nrows+1 , column=2))
    copycell(table.cell(row=nrows , column=3), table.cell(row=nrows+1 , column=3))
    copycell(table.cell(row=nrows , column=4), table.cell(row=nrows+1 , column=4))
    table.cell(row=nrows+1 , column=1).value = datestr
    table.cell(row=nrows+1 , column=2).value = rout
    table.cell(row=nrows+1 , column=3).value = rin
    table.cell(row=nrows+1 , column=4).value = rout-rin

def gdman():
    filenamegdin = getfilename('广东省漫入省份排行')
    filenamegdout = getfilename('广东省漫出省份排行')
    wbgdin = read_excel(filenamegdin, '','',0,'passwrod')
    wbgdin.rename(columns={'f':'漫入地市'},inplace=True)
    print(wbgdin)
    wbgdout = read_excel(filenamegdout, '','',0,'passwrod')
    wbgdout.rename(columns={'f':'漫入地市'},inplace=True)
    print(wbgdout)
    provincegdin = wbgdin[wbgdin['c'].isna()]
    print(provincegdin)
    provincegdout = wbgdout[wbgdout['c'].isna()]
    print(provincegdout)
    print(provincegdin['h'].sum())
    print(provincegdout['h'].sum())
    filenamesm = getfilename('省漫统计')
    print(filenamesm)
    data = openpyxl.load_workbook(filenamesm)
    print(data.get_named_ranges()) # 输出工作页索引范围
    print(data.get_sheet_names()) # 输出所有工作页的名称
    table = data.get_sheet_by_name('广东')
    # table = data.active
    print(table.title) # 输出表名
    nrows = table.max_row # 获得行数
    ncolumns = table.max_column # 获得行数
    # table.cell(row=nrows+1 , column=1)= copy(table.cell(row=nrows , column=1))
    copycell(table.cell(row=nrows , column=1), table.cell(row=nrows+1 , column=1))
    copycell(table.cell(row=nrows , column=2), table.cell(row=nrows+1 , column=2))
    copycell(table.cell(row=nrows , column=3), table.cell(row=nrows+1 , column=3))
    table.cell(row=nrows+1 , column=1).value = getdate(filenamegdin)
    table.cell(row=nrows+1 , column=2).value = provincegdout['h'].sum()
    table.cell(row=nrows+1 , column=3).value = provincegdin['h'].sum()

    table = data.get_sheet_by_name('广东SM TOP5')
    # table = data.active
    print(table.title) # 输出表名
    nrows = table.max_row # 获得行数
    datenum = getdatenum(filenamegdin)
    for i in range(1,6):
        table.cell(row=nrows+i , column=1).value = i
        table.cell(row=nrows+i , column=2).value = provincegdin.iloc[[i-1],[1]].values[0][0]
        table.cell(row=nrows+i , column=3).value = provincegdin.iloc[[i-1],[7]].values[0][0]
        table.cell(row=nrows+i , column=4).value = '漫入'
        table.cell(row=nrows+i , column=5).value = datenum
    for i in range(1,6):
        table.cell(row=nrows+5+i , column=1).value = i
        table.cell(row=nrows+5+i , column=2).value = provincegdout.iloc[[i-1],[4]].values[0][0]
        table.cell(row=nrows+5+i , column=3).value = provincegdout.iloc[[i-1],[7]].values[0][0]
        table.cell(row=nrows+5+i , column=4).value = '漫出'
        table.cell(row=nrows+5+i , column=5).value = datenum

    table = data.get_sheet_by_name('北京')
    # table = data.active
    print(table.title) # 输出表名
    strdate = getdate(filenamegdin)
    city = '北京市'
    bjout = provincegdout[provincegdout['e']==city]
    print(bjout.iloc[[0],[7]].values[0][0],bjout)
    bjin = provincegdin[provincegdin['b']==city]
    print(bjin.iloc[[0],[7]].values[0][0],bjin)
    bjsh(table,strdate,bjout.iloc[[0],[7]].values[0][0],bjin.iloc[[0],[7]].values[0][0])
    table = data.get_sheet_by_name('上海')
    # table = data.active
    print(table.title) # 输出表名
    # strdate = getdate
    city = '上海市'
    bjout = provincegdout[provincegdout['e']==city]
    print(bjout.iloc[[0],[7]].values[0][0],bjout)
    bjin = provincegdin[provincegdin['b']==city]
    print(bjin.iloc[[0],[7]].values[0][0],bjin)
    bjsh(table,strdate,bjout.iloc[[0],[7]].values[0][0],bjin.iloc[[0],[7]].values[0][0])

    gzfilenamegdin = getfilename('广州市漫入省份排行')
    gzfilenamegdout = getfilename('广州市漫出省份排行')
    gzwbgdin = read_excel(gzfilenamegdin, '','',0,'passwrod')
    gzwbgdin.rename(columns={'f':'漫入地市'},inplace=True)
    print(gzwbgdin)
    gzwbgdout = read_excel(gzfilenamegdout, '','',0,'passwrod')
    gzwbgdout.rename(columns={'f':'漫入地市'},inplace=True)
    print(gzwbgdout)
    gzprovincegdin = gzwbgdin[gzwbgdin['c'].isna()]
    print(gzprovincegdin)
    gzprovincegdout = gzwbgdout[gzwbgdout['漫入地市'].isna()]
    print(gzprovincegdout)
    print(gzprovincegdin['h'].sum())
    print(gzprovincegdout['h'].sum())
    szgzfilenamegdin = getfilename('深圳市漫入省份排行')
    szgzfilenamegdout = getfilename('深圳市漫出省份排行')
    szgzwbgdin = read_excel(szgzfilenamegdin, '','',0,'passwrod')
    szgzwbgdin.rename(columns={'f':'漫入地市'},inplace=True)
    print(szgzwbgdin)
    szgzwbgdout = read_excel(szgzfilenamegdout, '','',0,'passwrod')
    szgzwbgdout.rename(columns={'f':'漫入地市'},inplace=True)
    print(szgzwbgdout)
    szgzprovincegdin = szgzwbgdin[szgzwbgdin['c'].isna()]
    print(szgzprovincegdin)
    szgzprovincegdout = szgzwbgdout[szgzwbgdout['漫入地市'].isna()]
    # print(gzprovincegdout)
    # print(gzprovincegdin['h'].sum())
    # print(gzprovincegdout['h'].sum())
    pinnum = gzprovincegdin.iloc[[1],[0]].values[0][0]
    # print(gzwbgdin[1:int(pinnum-1)])
    gzin = gzwbgdin[1:int(pinnum-1)]
    print(gzin['h'].sum())
    print(gzprovincegdin)
    table = data.get_sheet_by_name('广州(漫入)')
    # table = data.active
    print(table.title) # 输出表名
    # print(table[0:2])
    gz(table,strdate, gzprovincegdin['h'].sum(),gzprovincegdin.iloc[[0],[7]].values[0][0])
    table = data.get_sheet_by_name('广州(漫出)')
    # table = data.active
    print(table.title) # 输出表名
    # print(table[0:2])
    gz(table,strdate, gzprovincegdout['h'].sum(),gzprovincegdout.iloc[[0],[7]].values[0][0])
    table = data.get_sheet_by_name('深圳(漫入)')
    # table = data.active
    print(table.title) # 输出表名
    # print(table[0:2])
    gz(table,strdate, szgzprovincegdin['h'].sum(),szgzprovincegdin.iloc[[0],[7]].values[0][0])
    table = data.get_sheet_by_name('深圳(漫出)')
    # table = data.active
    print(table.title) # 输出表名
    # print(table[0:2])
    gz(table,strdate, szgzprovincegdout['h'].sum(),szgzprovincegdout.iloc[[0],[7]].values[0][0])

    print(gzprovincegdin)
    
    table = data.get_sheet_by_name('广州漫入 TOP5')
    # table = data.active
    print(table.title) # 输出表名
    nrows = table.max_row # 获得行数
    datenum = getdatenum(filenamegdin)
    for i in range(1,7):
        table.cell(row=nrows+i , column=1).value = i
        table.cell(row=nrows+i , column=2).value = gzprovincegdin.iloc[[i],[1]].values[0][0]
        table.cell(row=nrows+i , column=3).value = gzprovincegdin.iloc[[i],[7]].values[0][0]
        table.cell(row=nrows+i , column=4).value = '省外'
        table.cell(row=nrows+i , column=5).value = datenum
    print(gzwbgdin)
    for i in range(1,7):
        table.cell(row=nrows+6+i , column=1).value = i
        table.cell(row=nrows+6+i , column=2).value = gzwbgdin.iloc[[i],[2]].values[0][0]
        table.cell(row=nrows+6+i , column=3).value = gzwbgdin.iloc[[i],[7]].values[0][0]
        table.cell(row=nrows+6+i , column=4).value = '省内'
        table.cell(row=nrows+6+i , column=5).value = datenum

    table = data.get_sheet_by_name('广州漫出 TOP5')
    # table = data.active
    print(table.title) # 输出表名
    nrows = table.max_row # 获得行数
    datenum = getdatenum(filenamegdin)
    for i in range(1,7):
        table.cell(row=nrows+i , column=1).value = i
        table.cell(row=nrows+i , column=2).value = gzprovincegdout.iloc[[i],[4]].values[0][0]
        table.cell(row=nrows+i , column=3).value = gzprovincegdout.iloc[[i],[7]].values[0][0]
        table.cell(row=nrows+i , column=4).value = '省外'
        table.cell(row=nrows+i , column=5).value = datenum
    print(gzwbgdout)
    for i in range(1,7):
        table.cell(row=nrows+6+i , column=1).value = i
        table.cell(row=nrows+6+i , column=2).value = gzwbgdout.iloc[[i],[5]].values[0][0]
        table.cell(row=nrows+6+i , column=3).value = gzwbgdout.iloc[[i],[7]].values[0][0]
        table.cell(row=nrows+6+i , column=4).value = '省内'
        table.cell(row=nrows+6+i , column=5).value = datenum

    # table.cell(row=nrows + 1, column=1, value=table.cell(row=nrows , column=1).value)
    savefile(wbgdin,filenamegdin)
    savefile(wbgdout,filenamegdout)
    savefile(gzwbgdin,gzfilenamegdin)
    savefile(gzwbgdout,gzfilenamegdout)
    savefile(szgzwbgdin,szgzfilenamegdin)
    savefile(szgzwbgdout,szgzfilenamegdout)
    data.save(datenum+'省漫统计.xlsx')



# ————————————————
# 版权声明:本文为CSDN博主「JMXGODLZ」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
# 原文链接:https://blog.csdn.net/qq_40676033/article/details/86555425
    # wb = xlrd.open_workbook(filenamesm, formatting_info=True)
    # xwb = copy(wb)
    # sheet = xwb.get_sheet('第一个sheet')
    # rows = sheet.get_rows()
    # length = len(rows)
    # print(length)  # 输出 100



gdman()

#定义计算
def compute():
    file_dir="."
    # newworkbook=xlwt.Workbook(encoding='utf-8')
    # newworksheet=newworkbook.add_sheet('files')
    style = xlwt.XFStyle()
    # newworksheet.write(0,0,'文件名')
    # newworksheet.write(0,1,'行数')
    xlAPP=win32.Dispatch("Excel.Application")
    xlAPP.Visible=False
    iter=-1
    password= 'passwrod'
    for root,dirs,files in os.walk(file_dir):
        for name in files:
            iter+=1
            print(name)
            filename=os.path.join(root,name)
            # newworksheet.write(iter+1,0,label=filename)
            # if name.find('')
            # xls=xlAPP.Workbooks.Open(filename,False,True,None,Password=password)
            # sheet=xls.Worksheets(1)
            # newworksheet.write(iter+1,1,label=sheet.UsedRange.Rows.Count-1)
            # xls.Close()
    # newworkbook.save('d:\\'+a+'.xls')
    
#定义第三模块
# bt3 = tk.Button(window, text='自动计算', width=15, height=2, command=shurupassword and compute)
# bt3.pack()

# window.mainloop()               # 进入消息循环

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