如何复制 csv 内容并粘贴到 Excel sheet?
How to copy csv contents and paste to Excel sheet?
我正在使用 python v3 xlwings 库与 MS Excel 交互。我有一个 csv 文件 ToCopy.csv
,我想将这个 cvs 文件的全部内容复制到一个 Excel 文件中。
import xlwings as xw
Book_name = 'C:/Temp/ExcelBook.xlsm'
sheet_name = 'SheetName' #paste into this sheet
wb = xw.Book(Book_name)
sht = wb.sheets[sheet_name]
我查看了 xlwings 文档,但仍然不知道如何开始。谁能给我一个先机?
我愿意使用不基于 xlwings 库的其他方法。但是,我的 Excel 文件受密码保护,我知道 xlwings 可以处理受密码保护的 Excel 文件。我不确定其他方法。
既然你写道你已经尝试过 xlsxwriter 这可能行不通,但你是否考虑过 openpyxl?
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
df = pd.read_csv('ToCopy.csv', header=None, index_col=None)
wb = load_workbook(filename='C:/Temp/ExcelBook.xlsm')
ws = wb.create_sheet()
ws.title = 'ToCopy'
for r in dataframe_to_rows(df, index=False, header=False):
ws.append(r)
wb.save('C:/Temp/ExcelBook.xlsm')
所以根据文档 http://docs.xlwings.org/en/stable/quickstart.html
Reading/writing values to/from ranges is as easy as:
>>> sht.range('A1').value = 'Foo 1'
>>> sht.range('A1').value
'Foo 1' There are many convenience features available, e.g. Range expanding:
>>> sht.range('A1').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
>>> sht.range('A1').expand().value
[['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
在第一个示例中,一个值被写入位置 A1。
在第二个中,值被写入 A1、B1、C1 和 A2、B2、C2
所以你可能想做类似的事情
for row,line in enumerate(inputcsv):
myrange = 'A'+row
mydata = line.split(',')
sht.range(myrange).value = mydata
其他选项:
- Excel 将直接导入 csv - 手动导入
- VBA excel 中的宏导入文件,
通过 xlwing
触发
尝试打开csv并直接另存为xlsx:
import xlwings as xw
wb = xw.Book('C:\source.csv') # Connect to an existing file
# wb = xw.Book(r'C:\Users\hhsg\Desktop\target.xlsx') # On Windows: use raw strings to escape backslashes
wb.save('C:\target.xlsx')
# wb.save(r'C:\target.xlsx') # On Windows: use raw strings to escape backslashes
我正在使用 python v3 xlwings 库与 MS Excel 交互。我有一个 csv 文件 ToCopy.csv
,我想将这个 cvs 文件的全部内容复制到一个 Excel 文件中。
import xlwings as xw
Book_name = 'C:/Temp/ExcelBook.xlsm'
sheet_name = 'SheetName' #paste into this sheet
wb = xw.Book(Book_name)
sht = wb.sheets[sheet_name]
我查看了 xlwings 文档,但仍然不知道如何开始。谁能给我一个先机?
我愿意使用不基于 xlwings 库的其他方法。但是,我的 Excel 文件受密码保护,我知道 xlwings 可以处理受密码保护的 Excel 文件。我不确定其他方法。
既然你写道你已经尝试过 xlsxwriter 这可能行不通,但你是否考虑过 openpyxl?
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
df = pd.read_csv('ToCopy.csv', header=None, index_col=None)
wb = load_workbook(filename='C:/Temp/ExcelBook.xlsm')
ws = wb.create_sheet()
ws.title = 'ToCopy'
for r in dataframe_to_rows(df, index=False, header=False):
ws.append(r)
wb.save('C:/Temp/ExcelBook.xlsm')
所以根据文档 http://docs.xlwings.org/en/stable/quickstart.html
Reading/writing values to/from ranges is as easy as:
>>> sht.range('A1').value = 'Foo 1' >>> sht.range('A1').value
'Foo 1' There are many convenience features available, e.g. Range expanding:
>>> sht.range('A1').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]] >>> sht.range('A1').expand().value [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
在第一个示例中,一个值被写入位置 A1。 在第二个中,值被写入 A1、B1、C1 和 A2、B2、C2
所以你可能想做类似的事情
for row,line in enumerate(inputcsv):
myrange = 'A'+row
mydata = line.split(',')
sht.range(myrange).value = mydata
其他选项:
- Excel 将直接导入 csv - 手动导入
- VBA excel 中的宏导入文件, 通过 xlwing 触发
尝试打开csv并直接另存为xlsx:
import xlwings as xw
wb = xw.Book('C:\source.csv') # Connect to an existing file
# wb = xw.Book(r'C:\Users\hhsg\Desktop\target.xlsx') # On Windows: use raw strings to escape backslashes
wb.save('C:\target.xlsx')
# wb.save(r'C:\target.xlsx') # On Windows: use raw strings to escape backslashes