[英]Modify an existing Excel file using Openpyxl in Python
I am basically trying to copy some specific columns from a CSV file and paste those in an existing excel file[*.xlsx] using python.我基本上是想从 CSV 文件中复制一些特定的列,然后使用 python 将它们粘贴到现有的 excel 文件 [*.xlsx] 中。 Say for example, you have a CSV file like this :
比如说,你有一个像这样的 CSV 文件:
col_1 col_2 col_3 col_4
1 2 3 4
5 6 7 8
9 10 11 12
So, i wanted to copy the both col_3 and col_4 and paste those in col_8 and col_9 in an existing excel file [which is a .XLSX format].所以,我想复制 col_3 和 col_4 并将 col_8 和 col_9 中的那些粘贴到现有的 excel 文件中 [这是一种 .XLSX 格式]。 I have tried this in various way to solve, but could not find out the exact way.
我已经尝试过以各种方式解决这个问题,但找不到确切的方法。 i tried something like this :
我试过这样的事情:
with open( read_x_csv, 'rb') as f:
reader = csv.reader(f)
for row in reader:
list1 = row[13]
queue1.append(list1)
list2 = row[14]
queue2.append(list2)
list3 = row[15]
queue3.append(list3)
list4 = row[16]
queue4.append(list4)
and then接着
rb = open_workbook("Exact file path.....")
wb = copy(rb)
ws = wb.get_sheet(0)
row_no = 0
for item in queue1:
if(item != ""):
ii = int(item)
ws.write(row_no,12,ii)
row_no = row_no + 1
#ws.write(item)
print item
else:
ws.write(row_no,12,item)
row_no = row_no + 1
wb.save("Output.xls")
but problem with this solution is it does not allow me to save as *.XLSX format which is strictly required for me.但是这个解决方案的问题是它不允许我保存为 *.XLSX 格式,这是我严格要求的。
I have tried to use Openpyxl as it can handle *.XLSX format, but could not find out a way to modify the existing excel file.我尝试使用 Openpyxl,因为它可以处理 *.XLSX 格式,但找不到修改现有 excel 文件的方法。 can anyone please help on this?
任何人都可以帮忙吗?
Doubt : 1) Can we really read a whole column from a CSV file and store into an array/list using python?疑问:1)我们真的可以从 CSV 文件中读取一整列并使用 python 存储到数组/列表中吗? 2) Can we modify the existing excel file which is in .XLSX format using openpyxl or any other package?
2)我们可以使用openpyxl或任何其他包修改.XLSX格式的现有excel文件吗?
You can try the following implementation您可以尝试以下实现
from openpyxl import load_workbook
import csv
def update_xlsx(src, dest):
#Open an xlsx for reading
wb = load_workbook(filename = dest)
#Get the current Active Sheet
ws = wb.get_active_sheet()
#You can also select a particular sheet
#based on sheet name
#ws = wb.get_sheet_by_name("Sheet1")
#Open the csv file
with open(src) as fin:
#read the csv
reader = csv.reader(fin)
#enumerate the rows, so that you can
#get the row index for the xlsx
for index,row in enumerate(reader):
#Assuming space separated,
#Split the row to cells (column)
row = row[0].split()
#Access the particular cell and assign
#the value from the csv row
ws.cell(row=index,column=7).value = row[2]
ws.cell(row=index,column=8).value = row[3]
#save the csb file
wb.save(dest)
Can we really read a whole column from a CSV file and store into an array/list using python?我们真的可以从 CSV 文件中读取一整列并使用 python 存储到数组/列表中吗? No, because files are read sequentially, csv reader cannot read a column of data to a row.
不会,因为文件是按顺序读取的,所以csv阅读器不能把一列数据读到一行。 Instead you may read the whole content and use izip and islice to get a particular column.
相反,您可以阅读整个内容并使用 izip 和 islice 来获取特定列。 You can also use numpy.array
你也可以使用 numpy.array
Can we modify the existing excel file which is in .XLSX format using openpyxl or any other package?我们可以使用 openpyxl 或任何其他包修改 .XLSX 格式的现有 excel 文件吗? Yes, see the example above
是的,看上面的例子
from openpyxl import load_workbook
# Class to manage excel data with openpyxl.
class Copy_excel:
def __init__(self,src):
self.wb = load_workbook(src)
self.ws = self.wb.get_sheet_by_name("Sheet1")
self.dest="destination.xlsx"
# Write the value in the cell defined by row_dest+column_dest
def write_workbook(self,row_dest,column_dest,value):
c = self.ws.cell(row = row_dest, column = column_dest)
c.value = value
# Save excel file
def save_excel(self) :
self.wb.save(self.dest)
As it is 2021, get_sheet_by_name
is deprecated and raises an DeprecationWarning
with the following message: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
由于是 2021 年,不推荐使用
get_sheet_by_name
并引发DeprecationWarning
并显示以下消息: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
The following snippet can be used in order to not raise the warning.为了不引发警告,可以使用以下代码段。
from openpyxl import load_workbook
file_path = 'test.xlsx'
wb = load_workbook(file_path)
ws = wb['SHEET_NAME'] # or wb.active
ws['G6'] = 123
wb.save(file_path)
load_workbook(...)
)load_workbook(...)
) As simple as that!就如此容易!
from openpyxl import load_workbook
wb = load_workbook('test.xlsx')
See docs: https://openpyxl.readthedocs.io/en/stable/tutorial.html#loading-from-a-file请参阅文档: https : //openpyxl.readthedocs.io/en/stable/tutorial.html#loading-from-a-file
work_sheet = wb.active # Get active sheet
work_sheet.append(['John', 'Customer', 'He likes football'])
test.xlsx
test.xlsx
保存修改后的工作簿wb.save('test.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.