当前位置: 首页 > 后端技术 > Python

Python快速将源excel表格内容导入目标excel表格

时间:2023-03-26 14:38:17 Python

1.excel表格格式1.1.源excel表格内容Sheet1IDModuleOwnerResultCommentC000001LTE郭靖Support默认支持C000002TD-SCDMA郭靖NotSupport硬件不支持C000004WCMDA郭靖SupportC000005CDMA2000郭靖NotSupportC000006HSDPA郭靖SupportC000007EDGE郭靖SupportC000008GPRS郭靖SupportC000009GSM郭靖SupportC000010WIFI黄蓉SupportC000011FM黄蓉SupportC000012GPS黄蓉SupportC000013RFID杨过NotSupportC000014NFC杨过SupportSheet2IDModuleOwnerResultCommentC000001Call令狐冲SupportC000003Message令狐冲SupportC000004Music萧峰NotSupport价格太高未预置1.2.目标excel表格内容Sheet1IDModuleOwnerResultCommentC000001C000002C000003C000004C000005C000006C000007C000008C000009C000010C000011C000012C000013C000014Sheet2IDModuleOwnerResultCommentC000001C000002C000003C0000042.Howtoimportthecontentofthesourceexcelsheettothetargetexcelsheet2.1.Designtheconfigurationfiletobeimportedconfig.ini[Sheet1]srcKeyColumn=AdstKeyColumn=AsrcValueColumnStart=BdstValueColumnStart=Bcycle=4[Sheet2]srcKeyColumn=AdstKeyColumn=AsrcValueColumnStart=BdstValueColumnStart=Bcycle=42.2。Python程序excel_fill.pyimportconfigparserimportopenpyxlclassReadConfig(object):defget_excel_column_number(self,column):'''Parameters----------column:string表示excel表中列的字符String,如果字符串是字母,会转成数字,如果是数字串,会直接转回Returns------TYPEnumber。'''ifcolumn.isalpha():returnopenpyxl.utils.column_index_from_string(column)else:returnint(column)defget_config(self,configfile):'''参数----------配置文件:字符串配置文件名。下例[Sheet0]---表示excel表格(两个一个,一个是源文件,一个是目标文件)Sheet0pagesrcKeyColumn=1---表示源excel表的key所在的列dstKeyColumn=1---表示目标excel表的key所在的列srcValueColumnStart=27---表示源excel表格的值开始列dstValueColumnStart=27---表示目标excel的值开始列形式cycle=4---表示一共有几列值需要复制返回-------dict_index:list将上面的配置文件生成一个列表,列表中的每一项都是dict类型,方便程序使用。'''config=configparser.ConfigParser()config.read(configfile,encoding='utf-8')dict_index=[]forsheetinconfig.sections():dict_pair={}dict_pair['sheet']=sheetsrckeylist=[]forsrckeyinconfig.get(sheet,'srcKeyColumn').split(','):key=srckey.strip()srckeylist.append(self.get_excel_column_number(key))dict_pair['srcKeyColumn']=srckeylistdstkeylist=[]fordstkeyinconfig.get(sheet,'dstKeyColumn').split(','):key=dstkey.strip()dstkeylist.append(self.get_excel_column_number(key))dict_pair['dstKeyColumn']=dstkeylistdict_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)returndict_indexclassCopyExcel(object):defread_key_and_value_from_src(self,sheet,key_list,content_column):'''参数----------sheet:stringsourceexcelform工作表页面的名称。key_list:list在excel表格中,以若干列的组合作为key,list包含列数。content_column:number要读取的excel表格的列。返回-------src_key_and_value:dict得到的键值对。'''src_key_and_value={}forrow_countinrange(1,sheet.max_row+1,1):key=""forkey_iteminkey_list:key=key+str(sheet.cell(row=row_count,column=key_item).value)值=sheet.cell(row=row_count,column=content_column).valueif(keynotin["",None])and(valuenotin["",None]):src_key_and_value[key]=value返回src_key_and_valuedefwrite_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:要写入的excel表的列数。src_key_and_value:dictkey-valuepair,根据匹配的key将value写入对应的列。返回------无。'''forrow_countinrange(1,sheet.max_row+1,1):key=""forkey_iteminkey_list:key=key+str(sheet.cell(row=row_count,column=key_item).value)if在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()foritemindict_index:src_sheet=src_wb[item['sheet_wb']dst[item['sheet']]foriinrange(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_s,item['dstKeyColumn'],item['dstValueColumnStart']+i,src_key_and_value)dst_wb.save(dst_excel_file)3.参考文档1.表格中的模块选自以下链接。在不打开射频模块的情况下如何工作?https://www.jianshu.com/p/9bf...2.Markdown教程https://www.runoob.com/马克多...