openpyxl如何将读入的数据写入某列
How to write the data that has been read into a certain column using openpyxl
我不知道如何将下面读取的数据写入某列,例如F列。B列已读取,我想将其粘贴到同一个工作簿中的F列中。最终我会创建一个函数,因为我会多次进行读写列。
import openpyxl
import os
# Finds current directory
current_path = os.getcwd()
print(current_path)
# Changes directory
os.chdir('C:\Users\Shane\Documents\Exel Example')
# prints new current directory
new_path = os.getcwd()
print(new_path)
# load workbook
wb = openpyxl.load_workbook('example.xlsx')
type(wb)
# load worksheet
ws1 = wb.active
# read sheet names
sht_names = wb.sheetnames
print(sht_names)
# ***reads and prints column B***
col_b = list(ws1.columns)[1]
for cellObj in col_b:
print(cellObj.value)
# write column b's contents into column F
from openpyxl import load_workbook
t = load_workbook("test.xlsx")
s = t.active # get the active worksheet
lst = list(s.columns)
lst_row = list(s.rows)
cellA1 = s[1][0]
rowA = s[1]
g = (x.value for x in lst[1]) # column b?
for item in g:
print (item)
我不知道如何将下面读取的数据写入某列,例如F列。B列已读取,我想将其粘贴到同一个工作簿中的F列中。最终我会创建一个函数,因为我会多次进行读写列。
import openpyxl
import os
# Finds current directory
current_path = os.getcwd()
print(current_path)
# Changes directory
os.chdir('C:\Users\Shane\Documents\Exel Example')
# prints new current directory
new_path = os.getcwd()
print(new_path)
# load workbook
wb = openpyxl.load_workbook('example.xlsx')
type(wb)
# load worksheet
ws1 = wb.active
# read sheet names
sht_names = wb.sheetnames
print(sht_names)
# ***reads and prints column B***
col_b = list(ws1.columns)[1]
for cellObj in col_b:
print(cellObj.value)
# write column b's contents into column F
from openpyxl import load_workbook
t = load_workbook("test.xlsx")
s = t.active # get the active worksheet
lst = list(s.columns)
lst_row = list(s.rows)
cellA1 = s[1][0]
rowA = s[1]
g = (x.value for x in lst[1]) # column b?
for item in g:
print (item)