excel_copy.py

# -*-coding:utf-8-*-
# __author__ == "DT"
# __date__ == "2019/01/23"


import os, sys, stat
import os.path
import xlrd
# import xlsxwriter
# from openpyxl import Workbook
from xlutils.copy import copy


def copy_sheet(from_xls, to_xls):
    wb = xlrd.open_workbook(from_xls, formatting_info=True)
    from_all_sheet = wb.sheet_names()

    wb1 = xlrd.open_workbook(to_xls)
    to_all_sheet = wb1.sheet_names()

    if "字段说明" in from_all_sheet:
        from_sheet = wb.sheet_by_name("字段说明")
        from_nrows = int(from_sheet.nrows)
        from_ncols = int(from_sheet.ncols)

        if "字段说明" in to_all_sheet:
            print(to_xls + "中已经存在,字段说明")

        else:
            old_to_xls = xlrd.open_workbook(to_xls, formatting_info=True)
            new_to_xls = copy(old_to_xls)
            add_sheet = new_to_xls.add_sheet('字段说明')

            for i in range(from_ncols):

                for n in range(from_nrows):
                    value = from_sheet.cell_value(n, i)
                    print("正在拷贝" + "第%d行" % n + "第%d列" % i + "数据:" + str(value))
                    add_sheet.write(n, i, value)

            new_to_xls.save(to_xls)
            os.chmod(to_src, stat.S_IREAD)
            print("\n数据拷贝完成")

    else:
        print((from_xls.split("\\"))[-1] + " 中没有'字段说明'表格")
        pass


if __name__ == '__main__':

    from_path = r'E:\Rose\DRAGONBALL_CHINA\Trunk\branch_DragonBall\Design\ConfigExcel'
    to_path = r'C:\1\excel'
    config_path = r"C:\1\config_xls_name.txt"

    config_file = open(config_path)
    f = config_file.readlines()
    from_list = os.listdir(from_path)
    to_list = os.listdir(to_path)
    for f_name in f:
        f_name = f_name.replace("\n", "")
        if f_name in from_list:
            if f_name in to_list:
                if ".xls" in f_name:
                    from_src = r'%s' % (from_path + "\\" + f_name)
                    to_src = r'%s' % (to_path + "\\" + f_name)

                    # print(f_name, from_src, to_src)
                    os.chmod(to_src, stat.S_IREAD+stat.S_IWRITE)
                    copy_sheet(from_src, to_src)
                else:
                    print(f_name + "文件不是excel文件")
            else:
                print("目标径没有文件: " + f_name)
        else:
            print("源路径没有文件: " + f_name)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/Users/zhengyaqi/opt/anaconda3/envs/py/bin/python /Users/zhengyaqi/PycharmProjects/pythonProject13/main.py Traceback (most recent call last): File "/Users/zhengyaqi/PycharmProjects/pythonProject13/main.py", line 32, in <module> find_and_copy_files(source_folder, target_folder, excel_file) File "/Users/zhengyaqi/PycharmProjects/pythonProject13/main.py", line 7, in find_and_copy_files excel_data = pd.read_excel(excel_file, engine='xlrd') File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 478, in read_excel io = ExcelFile(io, storage_options=storage_options, engine=engine) File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 1513, in __init__ self._reader = self._engines[engine](self._io, storage_options=storage_options) File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/pandas/io/excel/_xlrd.py", line 35, in __init__ super().__init__(filepath_or_buffer, storage_options=storage_options) File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 540, in __init__ self.book = self.load_workbook(self.handles.handle) File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/pandas/io/excel/_xlrd.py", line 48, in load_workbook return open_workbook(file_contents=data) File "/Users/zhengyaqi/opt/anaconda3/envs/py/lib/python3.10/site-packages/xlrd/__init__.py", line 170, in open_workbook raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported') xlrd.biffh.XLRDError: Excel xlsx file; not supported报错结果
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值