读取 CSV
using pandas.read_csv http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html提取信息
import pandas as pd
df = pd.read_csv(my_filename)
您可能需要指定的选项
- sep:使用哪个分隔符
- encoding
- header:第一行是一行标签吗?
- index_col:是索引的第一列
添加到 Excel 工作表
灵感来自:https://stackoverflow.com/a/20221655/1562285 https://stackoverflow.com/a/20221655/1562285检查pandas.to_excel http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html其他可能选项的文档
book = load_workbook(old_filename)
sheet_name = 'Sheet1'
with pd.ExcelWriter(new_filename, engine='openpyxl') as writer:
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=10, engine='openpyxl')
startrow 和 startcol 表示您想要在工作表中粘贴数据的位置。
此方法可能会覆盖此工作表上以前的内容。如果是这样,您将必须循环 DataFrame 的列和行并将它们半手动添加到工作表中
插入图像
如果您有要在外部插入的图像,您可以使用以下代码文档 http://openpyxl.readthedocs.io/en/latest/usage.html#inserting-an-image
from openpyxl.drawing.image import Image
ws = book['sheet_name_for_images']
ws['A1'] = 'You should see three logos below'
img = Image('logo.png')
# add to worksheet and anchor next to cells
ws.add_image(img, 'A1')
我没有对此进行测试,您可能需要在writer.sheets = ...