最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Google Drive API Batch Upload Fails with Latin-1 Encoding Error, Works Individually - Stack Overflow

programmeradmin3浏览0评论

I'm encountering a UnicodeEncodeError: 'latin-1' codec can't encode character '\ufffd' when attempting to upload Excel files to Google Drive using the Google Drive API's batch request functionality. The error specifically occurs in the batch request body.

Code Example:

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import os
from typing import List, Tuple, Dict

SERVICE_ACCOUNT_FILE = 'creds.json'

def get_drive_service():
    creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
    return build('drive', 'v3', credentials=creds)

def true_batch_upload_excel_files(file_pairs: List[Tuple[str, str]]) -> Dict[str, str]:
    drive_service = get_drive_service()
    file_id_mapping = {}
    batch_request = drive_service.new_batch_http_request()

    for idx, (excel_file_path, _) in enumerate(file_pairs):
        file_metadata = {
            'name': os.path.basename(excel_file_path).encode('utf-8').decode('utf-8'),
            'mimeType': 'application/vnd.google-apps.spreadsheet'
        }
        media = MediaFileUpload(
            excel_file_path,
            mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        )
        batch_request.add(
            drive_service.files().create(
                body=file_metadata,
                media_body=media,
                fields='id'
            )
        )
    try:
        batch_request.execute()
    except Exception as e:
        print(f"Error: {e}")

    return file_id_mapping

# Example usage
file_paths = [
    (r"C:\path\to\your\Sunflower.xlsx", r"C:\path\to\your\output.pdf"),
    (r"C:\path\to\your\AnotherFile.xlsx", r"C:\path\to\your\output2.pdf"),
    # ... more files ...
]
true_batch_upload_excel_files(file_paths)

Error Message:

UnicodeEncodeError: 'latin-1' codec can't encode character '\ufffd' in position 1961: Body ('') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

Observations:

  • When I upload the same files individually (without using a batch request), the issue does not occur.
  • The following individual upload works fine:
drive_service = get_drive_service()
file_metadata = {
    'name': os.path.basename(excel_file_path),
    'mimeType': 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload(
    excel_file_path,
    mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
file = drive_service.files().create(
    body=file_metadata,
    media_body=media,
    fields='id'
).execute()

Things I’ve Tried:

  1. Encoding the file names explicitly to UTF-8.
  2. Testing with simpler Excel files.
  3. Sanitizing the file content to replace \ufffd characters:
   if b'\ufffd' in content:
       content = content.replace(b'\ufffd', b'?')
       with open(temp_file_path, 'wb') as f:
           f.write(content)

Question:

Is there a known limitation with Google Drive API batch requests and encoding?
Is there a way to force UTF-8 encoding in this context?

Any insights or workarounds would be greatly appreciated.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论