简体   繁体   English

Python - GSPREAD - 将文本和格式从一张谷歌表复制到另一张

[英]Python - GSPREAD - Copy text and format from one google sheet to another one

I have two files in google drive (two google sheets) and I need to move from one file to another the data and the format in a regular mode.我在谷歌驱动器中有两个文件(两个谷歌表),我需要以常规模式将数据和格式从一个文件移动到另一个文件。

As you can see on the picture bellow, I have different text formats that I need to maintain (bold, text color, etc.):正如您在下图中看到的,我需要维护不同的文本格式(粗体、文本颜色等):

在此处输入图像描述

My first attempt was: Using only google drive sheet functions using IMPORTRANGE.我的第一次尝试是:使用 IMPORTRANGE 仅使用谷歌驱动表功能。 It copies the data very well but I loose the format that I want to mantain on the destination file.它很好地复制了数据,但我丢失了我想在目标文件中保留的格式。

My second attemp has been: Using Python and gspread package copy the data and the format from source google sheet to the destination one.我的第二次尝试是:使用 Python 和 gspread package 将数据和格式从源谷歌表复制到目标表。 Fo that I have the following code:我有以下代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
   
source_file_sheet = 'https://docs.google.com/spreadsheets/X'
destination_file_sheet = 'https://docs.google.com/spreadsheets/Y'
service_key = "file.json"
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_file = ServiceAccountCredentials.from_json_keyfile_name(service_key, scope)
sourceSheetName = cod_source_system_file_operational 
destinationSheetName = cod_source_system_file_billing 

client = gspread.authorize(creds_file)
spreadsheet_source = client.open_by_url(source_file_sheet)
spreadsheet_destination = client.open_by_url(destination_file_sheet)
sourceSheetId = spreadsheet_source.worksheet('Sheet1')
destinationSheetId = spreadsheet_destination.worksheet('Sheet2')
body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": sourceSheetId,
                    "startRowIndex": 3,
                    "endRowIndex": 10,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "destination": {
                    "sheetId": destinationSheetId,
                    "startRowIndex": 0,
                    "endRowIndex": 10,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "pasteType": "PASTE_NORMAL"
            }
        }
    ]
}
res = destinationSheetId.batch_update(body)
print(res)

But when I run this it gives me the following error:但是当我运行它时,它给了我以下错误:

Traceback (most recent call last):回溯(最近一次通话最后):

dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers

How can I solve my problem?我该如何解决我的问题?

Thanks for your help!谢谢你的帮助!

I believe your goal and your current situation as follows.我相信你的目标和你目前的情况如下。

  • You want to copy the values of the specific sheet in Google Spreadsheet "A" to the specific sheet in Google Spreadsheet "B".您想将 Google 电子表格“A”中特定工作表的值复制到 Google 电子表格“B”中的特定工作表。
  • You want to copy not only the values, but also the cell format.您不仅要复制值,还要复制单元格格式。
  • You want to achieve this using gspread for python.您想使用 gspread for python 来实现这一点。
  • You have already been able to get and put values for Google Spreadsheet using Sheets API.您已经能够使用表格 API 获取和放置 Google 电子表格的值。

Modification points:修改点:

  • Unfortunately, "CopyPasteRequest" of the batchUpdate method cannot copy from Google Spreadsheet to other Google Spreadsheet.遗憾的是,batchUpdate 方法的“CopyPasteRequest”无法从 Google 电子表格复制到其他 Google 电子表格。 It seems that this is the current specification.这似乎是当前的规范。

  • In order to copy not only the values, but also the cell format from Google Spreadsheet "A" to google Spreadsheet "B", I would like to propose the following flow.为了不仅将值,而且将单元格格式从谷歌电子表格“A”复制到谷歌电子表格“B”,我想提出以下流程。

    1. Copy the source sheet in the source Spreadsheet to the destination Spreadsheet.将源电子表格中的源工作表复制到目标电子表格。
    2. Copy the values with the format from the copied sheet to the destination sheet.将具有格式的值从复制的工作表复制到目标工作表。 And, delete the copied sheet.并且,删除复制的工作表。

When above points are reflected to a script, it becomes as follows.当以上几点反映到脚本时,它变成如下。

Sample script:示例脚本:

In this sample script, I prepared the script below client = gspread.authorize(credentials) as follows.在这个示例脚本中,我在client = gspread.authorize(credentials)下面准备了如下脚本。 Before you use this, please set the variables.在使用它之前,请设置变量。

client = gspread.authorize(credentials)

sourceSpreadsheetId = "###" # Please set the source Spreadsheet ID.
sourceSheetName = "Sheet1" # Please set the source sheet name.
destinationSpreadsheetId = "###" # Please set the destination Spreadsheet ID.
destinationSheetName = "Sheet2" # Please set the destination sheet name.

srcSpreadsheet = client.open_by_key(sourceSpreadsheetId)
srcSheet = srcSpreadsheet.worksheet(sourceSheetName)
dstSpreadsheet = client.open_by_key(destinationSpreadsheetId)
dstSheet = dstSpreadsheet.worksheet(destinationSheetName)

# 1. Copy the source sheet in the source Spreadsheet to the destination Spreadsheet.
copiedSheet = srcSheet.copy_to(destinationSpreadsheetId)
copiedSheetId = copiedSheet["sheetId"]

# 2. Copy the values with the format from the copied sheet to the destination sheet. And, delete the copied sheet.
body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": copiedSheetId,
                    "startRowIndex": 3,
                    "endRowIndex": 10,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "destination": {
                    "sheetId": dstSheet.id,
                    "startRowIndex": 0,
                    "endRowIndex": 10,
                    "startColumnIndex": 0,
                    "endColumnIndex": 5
                },
                "pasteType": "PASTE_NORMAL"
            }
        },
        {
            "deleteSheet": {
                "sheetId": copiedSheetId
            }
        }
    ]
}
res = dstSpreadsheet.batch_update(body)
print(res)

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 使用 Google API 将电子表格工作表复制到另一个工作表 - Copy spreadsheet sheet to a another one with Google API using Python 在Python中将excel工作表从一个工作表复制到另一个工作表 - Copy excel sheet from one worksheet to another in Python Python 中从一个单元格到另一个单元格的 Google 表格超链接 - Google Sheet hyperlink from one cell to another in Python 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook Python Pandas在不更改任何数据的情况下将列从一张纸复制到另一张纸? - Python Pandas copy columns from one sheet to another sheet without changing any data? 将数据从一个 Excel 工作表复制到另一个 Excel 工作表 - Copy data from one excel sheet to another excel sheet 您如何使用 gspread 或其他方式将值从一个电子表格复制到另一个电子表格? - How do you copy values from one spreadsheet to another using gspread or some other way? 从一个 Google 表格复制和粘贴到另一个 - Copying and Pasting from one Google Sheet to another 尝试使用Python从一个文本文件复制到另一个文本文件 - Trying to copy from one text file to another in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM