8
n8n 中文网amn8n.com

使用Google Drive、OCR和OpenAI处理扫描发票到Google表格

高级

这是一个Invoice Processing, Multimodal AI领域的自动化工作流,包含 24 个节点。主要使用 If, Set, Html, Switch, Mailgun 等节点。 使用Google Drive、OCR和OpenAI处理扫描发票到Google表格

前置要求
  • Google Drive API 凭证
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "INSTANCE_ID_ANONYMIZED"
  },
  "nodes": [
    {
      "id": "85702921-bc6d-4f5c-a1d5-429fb0b969ac",
      "name": "当点击“执行工作流”时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -288,
        208
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "0455ad8a-32bd-4c3f-a944-02a4c9a60f74",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1808,
        720
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "CREDENTIAL_ID_001",
          "name": "OpenAI API Account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ec4fa0f9-5f94-4490-8404-0666e655f4bd",
      "name": "转换为文件",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        1312,
        224
      ],
      "parameters": {
        "options": {
          "fileName": "={{ $('Save base64').item.json.name }}"
        },
        "operation": "toBinary",
        "sourceProperty": "data"
      },
      "typeVersion": 1.1
    },
    {
      "id": "09323ac4-58c7-4260-a11b-9d06f133b29f",
      "name": "切换 MIME 类型",
      "type": "n8n-nodes-base.switch",
      "position": [
        880,
        400
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "image",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "fcf37995-e2b2-466d-a8a8-aad25380f329",
                    "operator": {
                      "type": "string",
                      "operation": "startsWith"
                    },
                    "leftValue": "={{ $binary.data.mimeType }}",
                    "rightValue": "image/"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "pdf",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0853b815-fd5d-413d-9f0e-0d2fc8dbdade",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $binary.data.mimeType }}",
                    "rightValue": "application/pdf"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "f15cfb3e-630b-47da-8318-dc58289f9e07",
      "name": "遍历项目",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        304,
        384
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "33e127d5-6a47-4637-bcb2-d135a9dae63b",
      "name": "设置文件标识",
      "type": "n8n-nodes-base.set",
      "position": [
        528,
        400
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f021928e-16df-4280-9e52-8c671e7e36d5",
              "name": "id",
              "type": "string",
              "value": "={{ $json.id }}"
            },
            {
              "id": "6cdca709-1b00-4970-b9bb-9c2184600a5e",
              "name": "name",
              "type": "string",
              "value": "={{ $json.name }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "89803795-7f66-495a-98fc-5024627a0242",
      "name": "保存 base64",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        704,
        400
      ],
      "parameters": {
        "options": {
          "keepSource": "both"
        },
        "operation": "binaryToPropery"
      },
      "typeVersion": 1
    },
    {
      "id": "2a32007a-337c-455c-9c96-52307e1efcc9",
      "name": "重新设置 base64",
      "type": "n8n-nodes-base.set",
      "position": [
        1152,
        224
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "efee376a-ed86-4895-8fc5-df2ff3b08a80",
              "name": "data",
              "type": "string",
              "value": "={{ $('Save base64').item.json.data }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "7424c2eb-c3c3-46ff-82e4-2404f5cfc7fe",
      "name": "检查新发票",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -288,
        384
      ],
      "parameters": {
        "event": "fileCreated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "GOOGLE_DRIVE_FOLDER_ID_001",
          "cachedResultUrl": "https://drive.google.com/drive/folders/GOOGLE_DRIVE_FOLDER_ID_001",
          "cachedResultName": "Scanned invoices"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "CREDENTIAL_ID_002",
          "name": "Google Drive Account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a6839ddd-aacc-405b-a145-dd2152f91f6e",
      "name": "下载文件",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        128,
        384
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {},
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "CREDENTIAL_ID_002",
          "name": "Google Drive Account"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "17d99786-f1e9-46e8-85a5-64d5d4ed65ad",
      "name": "获取 50 个文档",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -64,
        208
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list",
            "value": "GOOGLE_DRIVE_FOLDER_ID_001",
            "cachedResultUrl": "https://drive.google.com/drive/folders/GOOGLE_DRIVE_FOLDER_ID_001",
            "cachedResultName": "Scanned invoices"
          }
        },
        "options": {},
        "resource": "fileFolder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "CREDENTIAL_ID_002",
          "name": "Google Drive Account"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "3ad34319-3715-4ff0-bf90-bfdd0f3a27e8",
      "name": "从文件中提取",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        1152,
        480
      ],
      "parameters": {
        "options": {},
        "operation": "pdf"
      },
      "typeVersion": 1
    },
    {
      "id": "a7daca53-ed48-4af9-8c27-c18e4b5710e2",
      "name": "识别 CamScanner",
      "type": "n8n-nodes-base.if",
      "position": [
        1312,
        480
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "d074d65f-65d4-46b1-baa6-306a2ca1e827",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.info.Author }}",
              "rightValue": "CamScanner"
            },
            {
              "id": "3d23a529-7e71-489d-9997-0d3f22bc0927",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              },
              "leftValue": "={{ $json.text }}",
              "rightValue": "CamScanner"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a4df0641-b633-4ba4-bb43-31cacac9824b",
      "name": "分析图像",
      "type": "n8n-nodes-base.httpRequest",
      "maxTries": 2,
      "position": [
        1504,
        304
      ],
      "parameters": {
        "url": "https://api.ocr.space/parse/image",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "multipart-form-data",
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "file",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "data"
            },
            {
              "name": "OCREngine",
              "value": "2"
            },
            {
              "name": "detectOrientation",
              "value": "true"
            }
          ]
        },
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "CREDENTIAL_ID_003",
          "name": "OCR API"
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2
    },
    {
      "id": "4ac1d408-c1e7-4b8c-9be8-53612b0addfe",
      "name": "编辑字段",
      "type": "n8n-nodes-base.set",
      "position": [
        1664,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "32ec54fd-db5a-4bc6-bbe4-9264d3a72c56",
              "name": "text",
              "type": "string",
              "value": "={{ $json.ParsedResults[0].ParsedText }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "332f6947-3460-4dc6-984a-982a3829b121",
      "name": "信息提取器",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        1856,
        496
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {},
        "attributes": {
          "attributes": [
            {
              "name": "company name",
              "description": "The company which produced the invoice"
            },
            {
              "name": "total",
              "type": "number",
              "description": "The total price of the invoice"
            },
            {
              "name": "currency",
              "description": "The currency of the total sum as letters code or symbol"
            },
            {
              "name": "date",
              "description": "the date in which the invoice was generated"
            },
            {
              "name": "invoice number",
              "description": "a unique id of the invoice, can be a mixture of text and numbers"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "57343929-604b-4b2f-bbc4-3c601033538d",
      "name": "在表格中追加行",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2176,
        496
      ],
      "parameters": {
        "columns": {
          "value": {
            "id": "={{ $('Set file identity').last().json.id }}",
            "Scan": "=https://drive.google.com/file/d/{{ $('Set file identity').last().json.id }}/view?usp=drive_link",
            "date": "={{ $json.output.date }}",
            "total": "={{ $json.output.total }}",
            "currency": "={{ $json.output.currency }}",
            "scan name": "={{ $('Set file identity').last().json.name }}",
            "company name": "={{ $json.output['company name'] }}",
            "invoice number": "={{ $json.output['invoice number'] }}"
          },
          "schema": [
            {
              "id": "id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "company name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "company name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "total",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "currency",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "currency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "invoice number",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "invoice number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Scan",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Scan",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "scan name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "scan name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Context",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Context",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/GOOGLE_SHEETS_DOCUMENT_ID_001/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "GOOGLE_SHEETS_DOCUMENT_ID_001",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/GOOGLE_SHEETS_DOCUMENT_ID_001/edit?usp=drivesdk",
          "cachedResultName": "Expenses from scanned invoices"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "CREDENTIAL_ID_004",
          "name": "Google Sheets Account"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "96efa1fd-74e9-4242-8256-26185c021340",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1104,
        688
      ],
      "parameters": {
        "width": 380,
        "height": 220,
        "content": "### 需要时重定向到 OCR"
      },
      "typeVersion": 1
    },
    {
      "id": "670c7063-2d84-4b34-b99b-68414f55e27f",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        304
      ],
      "parameters": {
        "color": 2,
        "width": 460,
        "height": 80,
        "content": "### 为后续流程设置一些便捷值"
      },
      "typeVersion": 1
    },
    {
      "id": "21b66216-fd94-49cf-8050-0b165308c440",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1120,
        144
      ],
      "parameters": {
        "width": 696,
        "height": 308,
        "content": "### 用于图像和提取效果差的 PDF 的 OCR"
      },
      "typeVersion": 1
    },
    {
      "id": "ea1bc5c4-d8ed-47c0-a5b9-9bf6277342ca",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -352,
        16
      ],
      "parameters": {
        "width": 440,
        "height": 640,
        "content": "# 从这里开始"
      },
      "typeVersion": 1
    },
    {
      "id": "ed831e1b-15fa-42c4-9deb-7a347745ab92",
      "name": "Mailgun",
      "type": "n8n-nodes-base.mailgun",
      "position": [
        688,
        144
      ],
      "parameters": {
        "html": "=<h3>New invoices were scanned</h3>\n\n{{ $json.table }}",
        "subject": "New scanned invoices",
        "toEmail": "user@example.com",
        "fromEmail": "=n8n@example.com"
      },
      "credentials": {
        "mailgunApi": {
          "id": "CREDENTIAL_ID_005",
          "name": "Mailgun Account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0f1ad01f-96c7-4c49-a346-040088f40b6c",
      "name": "准备表格",
      "type": "n8n-nodes-base.html",
      "position": [
        528,
        144
      ],
      "parameters": {
        "options": {
          "capitalize": true,
          "customStyling": true
        },
        "operation": "convertToHtmlTable"
      },
      "typeVersion": 1.2
    },
    {
      "id": "8ba5e5ab-b8e8-4ec5-96fd-c9b541832b66",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        16
      ],
      "parameters": {
        "width": 340,
        "height": 112,
        "content": "# 到此结束"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save base64": {
      "main": [
        [
          {
            "node": "Switch Mime Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Image": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch 50 docs": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Re-set base64": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Analyze Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Prepare a table",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set file identity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare a table": {
      "main": [
        [
          {
            "node": "Mailgun",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Mime Type": {
      "main": [
        [
          {
            "node": "Analyze Image",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Identify CamScanner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Set file identity": {
      "main": [
        [
          {
            "node": "Save base64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Identify CamScanner": {
      "main": [
        [
          {
            "node": "Re-set base64",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check for new invoices": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking 'Execute workflow'": {
      "main": [
        [
          {
            "node": "Fetch 50 docs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 发票处理, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量24
分类2
节点类型16
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Ovadia Rocks

Ovadia Rocks

@zohar

I save my customers huge amounts of time and money by designing and delivering AI automations and AI-powered applications. With over 20 years in technical and management roles, I’ve led teams, launched products, and steered complex projects across industries and technologies. If you’re looking to harness AI for real business impact - not just experimentation - let’s connect.

外部链接
在 n8n.io 查看

分享此工作流