经过几年寻找问题答案之后,我终于加入了这里。到了我需要有关用例的一些指导的时候了。我有3种不同的.xlsx工作簿,我想做的是添加一列并将concat 2列放在一起以填充新添加的列。我想我已经做到了,但是需要做一些细微的调整。我的下一个问题是,我试图从添加了列和值的工作簿中复制一个范围,并将其粘贴到另一个具有特定单元格范围的工作簿中。我能够找到一些代码来完成此操作,但是我不认为它可以与我创建的数据框一起使用。
我对python世界还很陌生,所以请对我轻松一点。
import pandas as pd
import numpy as np
import openpyxl
df1 = pd.read_excel('C:/Users/u678153/Desktop/FIN_-_Find_Customer_Invoices_Lines_with_Worktags.xlsx', index_col=None,
sheet_name='Detail')
a = np.char.array(df1["Account Prefix (CBF)"].astype(object))
b = np.char.array(df1["IPPER Plan Code (CBF)"].astype(object))
df1.insert(15, 'CON/PLAN', (a+b))
#File to be copied
wb = openpyxl.load_workbook("'C:/Users/u678153/Desktop/07_19 Custom.xlsx'") #Add file name
sheet = wb.get_sheet_by_name("Static") #Add Sheet name
#File to be pasted into
template = openpyxl.load_workbook("C:/Users/u678153/Desktop/FIN_-_Find_Customer_Invoices_Lines_with_Worktags.xlsx")
temp_sheet = template.get_sheet_by_name("Detail")
#Copy range of cells as a nested list
#Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
rangeSelected = []
#Loops through selected Rows
for i in range(startRow,endRow + 1,1):
#Appends the row to a RowSelected list
rowSelected = []
for j in range(startCol,endCol+1,1):
rowSelected.append(sheet.cell(row = i, column = j).value)
#Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
return rangeSelected
#Paste range
#Paste data from copyRange into template sheet
def pasteRange(startCol, startRow, endCol, endRow, sheetReceiving,copiedData):
countRow = 0
for i in range(startRow,endRow+1,1):
countCol = 0
for j in range(startCol,endCol+1,1):
sheetReceiving.cell(row = i, column = j).value = copiedData[countRow][countCol]
countCol += 1
countRow += 1
def createData():
print("Processing...")
selectedRange = copyRange(2,2,25,2769,sheet)
pastingRange = pasteRange(1,2,25,2769,temp_sheet,selectedRange)
template.save("C:/Users/u678153/Desktop/test1.xlsx")
print("Range copied and pasted!")