openpyxl 追加到右边
openpyxl append to the right
我是 openpyxl 的新手,遇到了一些小问题
我正在遍历一个列表,并为每个项目添加一个标题和一些数据。
我的问题是,对于我通过的每个项目,我想将 "same" 数据添加到右侧,而不是像 .append 方法那样自上而下..
不知道有没有道理
一个例子
for agreement in agreements:
n1 = wb.active
n1.append([
make_cell(n1, agreement.name, bold=True),
])
n1.append([
'Account Number',
'Account Name',
'Total DKK',
])
for ....
n1.append(....)
对于每个协议,我想将 "same" 数据添加到右侧,而不是颠倒。
怎么做?
我想要的..
headline headline headline
Account Number Account Name Total DKK Account Number Account Name Total DKK Account Number Account Name Total DKK
Question: ... add the "same" data to the right, not top down like the .append
method
无论如何你必须使用ws.append(...)
。
首先你需要一个Workbook
实例:
from openpyxl import Workbook
wb = Workbook()
Don't use n1 = wb.active
inside your for ...
loop.
You have to get the active Worksheet only once.
for agreement in agreements:
n1 = wb.active
ws = wb.active
You want to add x Header per Row to the right, so you can't use .append(...)
.
n1.append(['Account Number', 'Account Name', 'Total DKK',])
在 .append(...)
行列值之前,您必须创建一个包含预期值的列表。
例如:
HEADER = ['Account Number', 'Account Name','Total DKK']
listOfColumnValues = []
# Extend listOfColumnValues with HEADER as many times you need
for _ in range(3):
listOfColumnValues.extend(HEADER)
print("{}".format(listOfColumnValues))
ws.append(listOfColumnValues)
Output:
['Account Number', 'Account Name', 'Total DKK', 'Account Number', 'Account Name', 'Total DKK', 'Account Number', 'Account Name', 'Total DKK']
测试 Python:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2
我是 openpyxl 的新手,遇到了一些小问题
我正在遍历一个列表,并为每个项目添加一个标题和一些数据。 我的问题是,对于我通过的每个项目,我想将 "same" 数据添加到右侧,而不是像 .append 方法那样自上而下..
不知道有没有道理
一个例子
for agreement in agreements:
n1 = wb.active
n1.append([
make_cell(n1, agreement.name, bold=True),
])
n1.append([
'Account Number',
'Account Name',
'Total DKK',
])
for ....
n1.append(....)
对于每个协议,我想将 "same" 数据添加到右侧,而不是颠倒。
怎么做?
我想要的..
headline headline headline
Account Number Account Name Total DKK Account Number Account Name Total DKK Account Number Account Name Total DKK
Question: ... add the "same" data to the right, not top down like the
.append
method
无论如何你必须使用ws.append(...)
。
首先你需要一个Workbook
实例:
from openpyxl import Workbook
wb = Workbook()
Don't use
n1 = wb.active
inside yourfor ...
loop.
You have to get the active Worksheet only once.for agreement in agreements: n1 = wb.active
ws = wb.active
You want to add x Header per Row to the right, so you can't use
.append(...)
.n1.append(['Account Number', 'Account Name', 'Total DKK',])
在 .append(...)
行列值之前,您必须创建一个包含预期值的列表。
例如:
HEADER = ['Account Number', 'Account Name','Total DKK']
listOfColumnValues = []
# Extend listOfColumnValues with HEADER as many times you need
for _ in range(3):
listOfColumnValues.extend(HEADER)
print("{}".format(listOfColumnValues))
ws.append(listOfColumnValues)
Output:
['Account Number', 'Account Name', 'Total DKK', 'Account Number', 'Account Name', 'Total DKK', 'Account Number', 'Account Name', 'Total DKK']
测试 Python:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2