Python/Excel自动化-文本从2个单元格变成1个单元格 [英] Python / Excel Automation - Text from 2 cells into 1 cell

查看:40
本文介绍了Python/Excel自动化-文本从2个单元格变成1个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python自动化使用Excel工作表列表创建多个名称标签的过程.

I'm using python to automate the process of creating multiple name tags using an excel sheet list.

我的问题是我需要获取'name'列和'enterprise'列值并将它们放在新文档的单个单元格中.

My problem is that I need to take the 'name' column and 'enterprise' column values ​​and put them in a single cell of a new document.

喜欢这个:

对此:

现在,我正在使用openpyxl,尽管我设法转移了其中一列,但我不能同时使用它们.下面的代码是我尝试过的事情之一.

Right now I'm using openpyxl and although I manage to transfer one of the columns I can't do it with both. The code below was one of the things that I've tried.

import openpyxl as xl

e = xl.load_workbook('etiquetas.xlsx')
eplan = e['Planilha1']

c = xl.load_workbook('Crachá Relação 15.10.19.xlsx')
cplan = c['Plan1']

maxlinhas = cplan.max_row

for i in range (2, maxlinhas+1):
    nome = cplan.cell(row = i, column = 1).value
    preenchernome = eplan.cell(row = i-1, column = 1)
    empresa = cplan.cell(row=i, column=2).value
    preencherempresa = eplan.cell(row=i - 1, column=1)
    preenchernome.value = nome, empresa
e.save('teste.xlsx')

但是此代码返回以下错误:

But this code returns the following error:

ValueError:无法将("Gladisson Garcia Westphal","Agro Divel")转换为Excel

ValueError: Cannot convert ('Gladisson Garcia Westphal', 'Agro Divel') to Excel

推荐答案

根据文档 preenchernome.value 只能有一个值

尝试使用此

preenchernome.value = '{}\n{}'.format(nome, empresa)

这篇关于Python/Excel自动化-文本从2个单元格变成1个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆