Создайте новую страницу в Документах Google программно с примерами кода

Чтобы создать новую страницу в Документах Google программным способом, вы можете использовать API Документов Google. Ниже приведены несколько методов на разных языках программирования вместе с примерами кода:

  1. Python:

    from googleapiclient.discovery import build
    from google.oauth2 import service_account
    # Authenticate and create a service object
    credentials = service_account.Credentials.from_service_account_file('credentials.json')
    service = build('docs', 'v1', credentials=credentials)
    # Create a new blank document
    document = service.documents().create().execute()
    document_id = document['documentId']
    # Insert a page break
    requests = [
    {
        'insertPageBreak': {
            'location': {
                'index': 1  # Insert page break at the second page
            }
        }
    }
    ]
    service.documents().batchUpdate(documentId=document_id, body={'requests': requests}).execute()
  2. JavaScript (Node.js):

    const { google } = require('googleapis');
    const credentials = require('./credentials.json');
    // Authenticate and create a client
    const auth = new google.auth.GoogleAuth({
    credentials,
    scopes: ['https://www.googleapis.com/auth/documents']
    });
    const docs = google.docs({ version: 'v1', auth });
    async function createNewPage() {
    try {
        // Create a new blank document
        const document = await docs.documents.create();
        const documentId = document.data.documentId;
        // Insert a page break
        const requests = [
            {
                insertPageBreak: {
                    location: {
                        index: 1  // Insert page break at the second page
                    }
                }
            }
        ];
        await docs.documents.batchUpdate({
            documentId,
            requestBody: {
                requests
            }
        });
    } catch (error) {
        console.error(error);
    }
    }
    createNewPage();

Обратите внимание, что в обоих примерах вам необходимо заменить 'credentials.json'путем к фактическому файлу учетных данных Google API.