(初学者)我正在尝试使用python将值从一个电子表格复制到另一个电子表格。我正在使用gspread,但似乎无法弄清楚如何将值从第一个电子表格复制到另一个电子表格。如何使用python复制第一个电子表格中的值并将其粘贴到另一个电子表格中?
这里是更新的代码:import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('sheetproject-956vg2854670.json',scope)
client = gspread.authorize(creds)
spreadsheetId= "1CkeZ8Xw4Xmho-mrFP9teHWVT-unkApzMSUFql5KkrGI"
sourceSheetName = "python test"
destinationSheetName = "python csv"
client = gspread.authorize(creds)
spreadsheet = client.open_by_key(spreadsheetId)
sourceSheetId = spreadsheet.worksheet("python test")._properties['0']
destinationSheetId = spreadsheet.worksheet("python csv")._properties['575313690']
body = {
"requests": [
{
"copypaste": {
"source": {
"0": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 20,
"startColumnIndex": 0,
"endcolumnIndex": 1
},
"destination": {
"575313690": destinationSheetId,
"startRowIndex": 0,
"endRowIndex": 20,
"startColumnIndex": 0,
"endcolumnIndex": 1
},
"pasteType": "Paste_Normal"
}
}
]
}
res = spreadsheet.batch_update(body)
print(res)
1)将openpyxl库导入为xl。
2)使用其所在的路径打开源excel文件。
注意:路径应为字符串,并具有双反斜杠(\)而不是单反斜杠()。例如:路径应为C:\ Users \ Desktop \ source.xlsx,而不是C:\ Users \ Admin \ Desktop \ source.xlsx
3)打开所需的工作表以使用其索引进行复制。工作表“ n”的索引是“ n-1”。例如,工作表1的索引为0。
4)打开目标excel文件和其中的活动工作表。
5)计算源excel文件中的行和列的总数。
6)使用两个for循环(一个用于循环访问excel文件的行,另一个用于循环访问excel文件的列)将源文件中的单元格值读取到变量,然后将其从该变量写入目标文件中的单元格。
7)保存目标文件。
这是来自How to copy over an Excel sheet to another workbook in Python的仅py代码示例:
import openpyxl as xl
path1 = 'C:\\Users\\Xukrao\\Desktop\\workbook1.xlsx'
path2 = 'C:\\Users\\Xukrao\\Desktop\\workbook2.xlsx'
wb1 = xl.load_workbook(filename=path1)
ws1 = wb1.worksheets[0]
wb2 = xl.load_workbook(filename=path2)
ws2 = wb2.create_sheet(ws1.title)
for row in ws1:
for cell in row:
ws2[cell.coordinate].value = cell.value
wb2.save(path2)
如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。
在此答案中,我想建议使用batch_update
将值从工作表复制到电子表格中的其他工作表。在这种情况下,可以通过一个API调用来实现您的目标。
在此示例脚本中,删除了授权脚本。显示了用于将值从工作表复制到电子表格中其他工作表的示例脚本。因此,当您使用此脚本时,请添加授权脚本,然后运行该脚本。
spreadsheetId = "###" # Please set the Spreadsheet ID.
sourceSheetName = "Sheet1" # Please set the sheet name of source sheet.
destinationSheetName = "Sheet2" # Please set the sheet name of destination sheet.
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sourceSheetId = spreadsheet.worksheet(sourceSheetName)._properties['sheetId']
destinationSheetId = spreadsheet.worksheet(destinationSheetName)._properties['sheetId']
body = {
"requests": [
{
"copyPaste": {
"source": {
"sheetId": sourceSheetId,
"startRowIndex": 0,
"endRowIndex": 5,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"destination": {
"sheetId": destinationSheetId,
"startRowIndex": 0,
"endRowIndex": 5,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"pasteType": "PASTE_VALUES"
}
}
]
}
res = spreadsheet.batch_update(body)
print(res)
A1:E5
的单元格的值复制到“ Sheet2”的工作表中A1:E5
的单元格。如果我误解了你的问题,而这不是你想要的方向,我深表歉意。