Python写入Excel文本和图片

import xlsxwriter

            
file_name = (u'{}.xlsx'.format(host_title))            
col_list = ['A', 'B', 'C']            
workbook = xlsxwriter.Workbook(file_name)            
worksheet = workbook.add_worksheet()            
goods_detail_url_list = self.goods_detail_url_list(host_link['host_link'])            
for m,goods_detail_url in enumerate(goods_detail_url_list):                
    goods_info_dict = self.parse_goods_info(goods_detail_url)              
    url = goods_info_dict['goods_detail_url']                
    title = goods_info_dict['title']                
    price = goods_info_dict['price'].replace('\n','').strip('')                
    img = goods_info_dict['img']                
    worksheet.set_column(0, 8, 20)  # 设置列宽20cm                
    worksheet.set_row(m + 1, 350)  # 设置行高350像素                
    for n in range(3):  # 列                    
        if m == 0 and n == 0:  # m行 n列                        
            worksheet.write('A1', 'title')                        
            worksheet.write('B1', 'price')                        
            worksheet.write('C1', 'url')                        
            worksheet.write('D1', 'img')                    
        else:                        
            if n == 0:                            
                worksheet.write(str(col_list[n]) + str(m + 2), title)                        
            elif n == 1:                            
                worksheet.write(str(col_list[n]) + str(m + 2), price)                        
            elif n == 2:                            
                worksheet.write(str(col_list[n]) + str(m + 2), url)                        
            else:                            
                param = {
                                'x_offset': 0,
                                'y_offset': m * 100,
                                'x_scale': 1,
                                'y_scale': 1,
                                "width": 100,
                                "height": 80,
                                'url': None,
                                'tip': None,
                                'image_data': None,
                                'positioning': None,
                            }                            
                try:                                                            
                    response = requests.get(img).content                                    
                    with open(file_path + 'temp.jpg', 'wb')as f:                                        
                        f.write(response)                                    
                    worksheet.insert_image('D' + str(m), file_path + 'temp.jpg', param)                                                     
                except Exception as e:                                
                    print(e)                                
                time.sleep(1)
    workbook.close()

 

你可能感兴趣的:(文件操作)