使用Python将csv文件中的多个列复制到现有xls文件

时间:2016-07-26 19:48:55

标签: python excel python-3.x csv

我对Python很陌生,但我在开始这方面遇到了一些困难。我使用的是Python 3。

我用Google搜索并找到了不少python模块,但是希望在这里提供更明确的答案。基本上,我需要从csv文件中读取某些列,即G,H,I,K和M.我需要的是不连续的。

我需要从csv文件中读取这些列,并将它们传输到现有xls中的空列,其中已有数据。

我查看了openpyxl,但它似乎不适用于csv / xls文件,只有xlsx。 我可以使用xlwt模块执行此操作吗?

对于哪个模块最适合我的用例的任何指导将不胜感激。与此同时,我将使用xlwt / xlrd修补。

2 个答案:

答案 0 :(得分:2)

我建议使用熊猫。它具有读取和写入csv和xls文件的便捷功能。

import pandas as pd
from openpyxl import load_workbook

#read the csv file
df_1 = pd.read_csv('c:/test/test.csv')

#lets say df_1 has columns colA and colB
print(df_1)

#read the xls(x) file
df_2=pd.read_excel('c:/test/test.xlsx')
#lets say df_2 has columns aa and bb

#now add a column from df_1 to df_2
df_2['colA']=df_1['colA']

#save the combined output
writer = pd.ExcelWriter('c:/test/combined.xlsx')
df_2.to_excel(writer)
writer.save()

#alternatively, if you want to add just one column to an existing xlsx file:

#i.e. get colA from df_1 into a new dataframe
df_3=pd.DataFrame(df_1['colA'])


#create writer using openpyxl engine
writer = pd.ExcelWriter('c:/test/combined.xlsx', engine='openpyxl') 

#need this workaround to provide a list of work sheets in the file
book = load_workbook('c:/test/combined.xlsx')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

column_to_write=16 #this would go to column Q (zero based index)
writeRowIndex=0 #don't plot row index
sheetName='Sheet1' #which sheet to write on

#now write the single column df_3 to the file
df_3.to_excel(writer, sheet_name=sheetName, columns =['colA'],startcol=column_to_write,index=writeRowIndex)

writer.save()

答案 1 :(得分:1)

您可以尝试XlsxWriter,它是用于编写Excel 2007+ XLSX文件格式的全功能python模块。 https://pypi.python.org/pypi/XlsxWriter