1. excel表格格式

1.1. 源excel表格内容

Sheet1

IDModuleOwnerResultComment
C000001LTE郭靖Support默认支持
C000002TD-SCDMA郭靖Not Support硬件不支持
C000004WCMDA郭靖Support
C000005CDMA2000郭靖Not Support
C000006HSDPA郭靖Support
C000007EDGE郭靖Support
C000008GPRS郭靖Support
C000009GSM郭靖Support
C000010WIFI黄蓉Support
C000011FM黄蓉Support
C000012GPS黄蓉Support
C000013RFID杨过Not Support
C000014NFC杨过Support

Sheet2

IDModuleOwnerResultComment
C000001Call令狐冲Support
C000003Message令狐冲Support
C000004Music萧峰Not Support价格太高未预置

1.2. 目标excel表格内容

Sheet1

IDModuleOwnerResultComment
C000001
C000002
C000003
C000004
C000005
C000006
C000007
C000008
C000009
C000010
C000011
C000012
C000013
C000014

Sheet2

IDModuleOwnerResultComment
C000001
C000002
C000003
C000004

2. 如何导入源excel表格的内容到目标excel表格

2.1. 设计要导入的配置文件

config.ini

[Sheet1]
srcKeyColumn = A
dstKeyColumn = A
srcValueColumnStart = B
dstValueColumnStart = B
cycle = 4

[Sheet2]
srcKeyColumn = A
dstKeyColumn = A
srcValueColumnStart = B
dstValueColumnStart = B
cycle = 4

2.2. Python程序

excel_fill.py

import configparser
import openpyxl

class ReadConfig(object):
    
    def get_excel_column_number(self, column):
        '''
        Parameters
        ----------
        column : string
            表示excel表格中列的字符串,该字符串如果为字母则转为数字,如果为数字字符串则直接转型

        Returns
        -------
        TYPE
            数字.
        '''
        if column.isalpha():
            return openpyxl.utils.column_index_from_string(column)
        else:
            return int(column)

    def get_config(self, configfile):
        '''
        Parameters
        ----------
        configfile : string
            配置文件名称.如下例子
            [Sheet0]                    ---表示excel表格(两个,一个是源文件,一个目标文件)的Sheet0页面
            srcKeyColumn = 1            ---表示源excel表格的key所在的列
            dstKeyColumn = 1            ---表示目标excel表格的key所在的列
            srcValueColumnStart = 27    ---表示源excel表格的value开始列
            dstValueColumnStart = 27    ---表示目标excel表格的value开始列
            cycle = 4                   ---表示总共有几列value需要拷贝

        Returns
        -------
        dict_index : list
            把上述配置文件生成为一个list,list里的每个项是dict类型,方便程序使用.
        '''
        config = configparser.ConfigParser()
        config.read(configfile, encoding='utf-8')
        dict_index = []
        for sheet in config.sections():
            dict_pair = {}
            dict_pair['sheet'] = sheet
            srckeylist = []
            for srckey in config.get(sheet,'srcKeyColumn').split(','):
                key = srckey.strip()
                srckeylist.append(self.get_excel_column_number(key))
            dict_pair['srcKeyColumn'] = srckeylist
            dstkeylist = []
            for dstkey in config.get(sheet,'dstKeyColumn').split(','):
                key = dstkey.strip()
                dstkeylist.append(self.get_excel_column_number(key))
            dict_pair['dstKeyColumn'] = dstkeylist
            dict_pair['srcValueColumnStart'] = self.get_excel_column_number(config.get(sheet,'srcValueColumnStart'))
            dict_pair['dstValueColumnStart'] = self.get_excel_column_number(config.get(sheet,'dstValueColumnStart'))
            dict_pair['cycle'] = int(config.get(sheet,'cycle'))
            dict_index.append(dict_pair)
        return dict_index


class CopyExcel(object):

    def read_key_and_value_from_src(self, sheet, key_list, content_column):
        '''
        Parameters
        ----------
        sheet : string
            源excel表格的sheet页面名称.
        key_list : list
            在excel表格中,以几列的组合作为key,这里list包含了几列的列数.
        content_column : number
            要读取的excel表格的列.

        Returns
        -------
        src_key_and_value : dict
            获取的键值对.
        '''
        src_key_and_value = {}
        for row_count in range(1, sheet.max_row + 1, 1):
            key = ""
            for key_item in key_list:
                key = key + str(sheet.cell(row = row_count, column = key_item).value)
            value = sheet.cell(row = row_count, column = content_column).value
            
            if (key not in ["", None]) and (value not in ["", None]):
                src_key_and_value[key] = value
        return src_key_and_value

    def write_value_to_dst_by_key(self, sheet, key_list, content_column, src_key_and_value):
        '''
        Parameters
        ----------
        sheet : string
            目标excel表格的sheet页面名称.
        key_list : list
            在excel表格中,以几列的组合作为key,这里list包含了几列的列数.
        content_column : number
            要写入的excel表格的列.
        src_key_and_value : dict
            键值对,根据匹配的键在对应column写入值.

        Returns
        -------
        None.

        '''
        for row_count in range(1, sheet.max_row + 1, 1):
            key = ""
            for key_item in key_list:
                key = key + str(sheet.cell(row = row_count, column = key_item).value)
                if key in src_key_and_value.keys():
                    #print(key)
                    sheet.cell(row = row_count, column = content_column, value=src_key_and_value[key])


if __name__ == '__main__':
    excel_config = ReadConfig()
    dict_index = excel_config.get_config('config.ini')

    src_excel_file = r"Src.xlsx"
    src_wb = openpyxl.load_workbook(src_excel_file)

    dst_excel_file = r"Dst.xlsx"
    dst_wb = openpyxl.load_workbook(dst_excel_file)

    copy_excel = CopyExcel()
    for item in dict_index:
        src_sheet = src_wb[item['sheet']]
        dst_sheet = dst_wb[item['sheet']]
        for i in range(item['cycle']):
            src_key_and_value = copy_excel.read_key_and_value_from_src(src_sheet, item['srcKeyColumn'], item['srcValueColumnStart'] + i)
            copy_excel.write_value_to_dst_by_key(dst_sheet, item['dstKeyColumn'], item['dstValueColumnStart'] + i, src_key_and_value)

    dst_wb.save(dst_excel_file)

3. 参考文档

1.表格中的模块选用了如下link的模块
手机的电话流量wifi等功能都离不开RF模块,它是怎么工作的?
https://www.jianshu.com/p/9bf...
2.Markdown教程
https://www.runoob.com/markdo...


无痕1024
1 声望0 粉丝