### Run Development Server Source: https://github.com/nightteam/invoice-manager/blob/master/README.md Starts the Django development server. Access the admin interface at http://127.0.0.1:8000/admin/ with default credentials 'admin'/'admin'. ```bash python manage.py runserver ``` -------------------------------- ### Invoice Manager Project Settings Source: https://context7.com/nightteam/invoice-manager/llms.txt Key configurations for the Invoice Manager project, including base directory, import and invoice storage paths, database settings, language, and time zone. This file also contains the full startup procedure. ```python from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent # 发票导入目录(放置待识别的 PDF 文件) IMPORT_PATH = Path("./import") # 发票存储目录(识别后的文件存放位置) INVOICES_PATH = Path("./invoices") # 数据库配置(默认 SQLite) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # 语言和时区 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' # 完整启动流程 # 1. 安装系统依赖 # - ImageMagick: https://imagemagick.org/script/download.php # (Windows 安装时勾选 "Install development headers and libraries for C and C++") # - Ghostscript: https://www.ghostscript.com/download/gsdnld.html # 2. 安装 Python 依赖 # pip install -r requirements.txt # # Django~=3.1.3 # # PyPDF4~=1.27.0 # # Wand~=0.6.3 # # requests~=2.25.0 # 3. 创建必要目录 # mkdir import invoices # 4. 初始化数据库 # python manage.py migrate # 5. 创建管理员账号 # python manage.py createsuperuser # 6. 启动服务 # python manage.py runserver # 7. 访问管理界面 # http://127.0.0.1:8000/admin/ ``` -------------------------------- ### Initialize Huawei Cloud OCR Client with Token Source: https://context7.com/nightteam/invoice-manager/llms.txt Uses username and password for temporary Token-based authentication. Tokens are valid for 24 hours and managed automatically by the client. ```python from huaweicloud_ocr_sdk.HWOcrClientToken import HWOcrClientToken import base64 # 初始化 OCR 客户端 domain_name = "your_domain" # 如果不是 IAM 用户,与 username 相同 username = "your_username" password = "your_password" region = "cn-north-4" # 支持 cn-north-1, cn-east-2 等区域 ocr_client = HWOcrClientToken(domain_name, username, password, region) # 识别增值税发票 req_uri = "/v1.0/ocr/vat-invoice" # 读取图片并转换为 base64 with open("invoice.png", "rb") as f: image_data = f.read() # 调用 OCR 服务 options = {} # 可选参数 response = ocr_client.request_ocr_service_base64(req_uri, image_data, options) if response.status_code == 200: result = response.json()["result"] print(f"发票号码: {result['number']}") print(f"开票日期: {result['issue_date']}") print(f"购买方名称: {result['buyer_name']}") print(f"购买方税号: {result['buyer_id']}") print(f"价税合计: {result['total']}") print(f"开票内容: {result['item_list']}") else: print(f"识别失败: {response.status_code} - {response.text}") ``` -------------------------------- ### Implement Invoice Import Script Source: https://context7.com/nightteam/invoice-manager/llms.txt Automates the process of scanning directories for PDF invoices, performing OCR, and saving data to the database. ```python import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'invoice_manager.settings') django.setup() from wand.image import Image from wand.color import Color from manager.models import Invoice from datetime import datetime from huaweicloud_ocr_sdk.HWOcrClientToken import HWOcrClientToken from invoice_manager.settings import IMPORT_PATH, INVOICES_PATH # 配置 OCR 客户端 username = "your_username" password = "your_password" domain_name = "your_domain" region = "cn-north-4" req_uri = "/v1.0/ocr/vat-invoice" ocrClient = HWOcrClientToken(domain_name, username, password, region) ``` -------------------------------- ### Initialize Huawei Cloud OCR Client with AK/SK Source: https://context7.com/nightteam/invoice-manager/llms.txt Uses Access Key and Secret Key for long-term authentication. This method is recommended for server-side applications to avoid frequent token refreshes. ```python from huaweicloud_ocr_sdk.HWOcrClientAKSK import HWOcrClientAKSK # 初始化 OCR 客户端(AK/SK 认证) ak = "your_access_key" sk = "your_secret_key" region = "cn-north-4" ocr_client = HWOcrClientAKSK(ak, sk, region) # 识别增值税发票 - 支持本地文件路径 req_uri = "/v1.0/ocr/vat-invoice" response = ocr_client.request_ocr_service_base64( req_uri, "/path/to/invoice.jpg", options={} ) # 也支持通过 URL 识别 response = ocr_client.request_ocr_service_base64( req_uri, "https://example.com/invoice.jpg", options={} ) if response.status_code == 200: result = response.json()["result"] print(f"识别成功: {result}") ``` -------------------------------- ### Configure Django Admin for Invoice Model Source: https://context7.com/nightteam/invoice-manager/llms.txt Defines the display, filtering, searching, and actions for the Invoice model in the Django admin interface. Custom actions like marking as used or changing category are listed. ```python from django.contrib import admin from .models import Invoice class InvoiceAdmin(admin.ModelAdmin): # 列表显示字段 list_display = ( "id", # 发票号码 "description", # 开票内容 "create_date", # 开票日期 "company_name", # 公司名 "company_id", # 税号 "price", # 金额 "used", # 使用状态 "category" # 发票类型 ) # 筛选器 list_filter = ["company_name", "company_id", "category", "used"] # 搜索字段 search_fields = ["id", "description"] # 批量操作 actions = [ use_invoices, # 标记为已使用 replace_category_to_交通费, # 设置为交通费 replace_category_to_餐饮费, # 设置为餐饮费 replace_category_to_未选择, # 重置类型 ] admin.site.register(Invoice, InvoiceAdmin) ``` -------------------------------- ### Define Invoice Data Model Source: https://context7.com/nightteam/invoice-manager/llms.txt The Invoice model stores key invoice details including identification, company information, and category. It uses Django's ORM to manage invoice records and status. ```python from django.db import models class Invoice(models.Model): # 发票号码(唯一ID) id = models.CharField(verbose_name="发票号码", primary_key=True, max_length=100) # 货物或应税劳务、服务名称(开票内容) description = models.CharField(verbose_name="开票内容", max_length=100) # 开票日期 create_date = models.DateField(verbose_name="开票日期") # 购买方名称(公司名) company_name = models.CharField(verbose_name="公司名", max_length=100) # 购买方纳税人识别号(税号) company_id = models.CharField(verbose_name="税号", max_length=100) # 价税合计(发票金额) price = models.FloatField(verbose_name="发票金额") # 是否已使用 used = models.BooleanField(verbose_name="已使用", default=False) # 发票类型选项 type_transportation_fee = "交通费" type_dining_fee = "餐饮费" type_not_selected = "未选择" categories = [ (type_transportation_fee, "交通费"), (type_dining_fee, "餐饮费"), (type_not_selected, "未选择"), ] # 所属类型 category = models.CharField( verbose_name="发票类型", max_length=100, choices=categories, default=type_not_selected ) # 使用示例:创建发票记录 from manager.models import Invoice from datetime import datetime invoice = Invoice( id="12345678", description="餐饮服务、外卖配送费", create_date=datetime(2024, 1, 15), company_name="某某科技有限公司", company_id="91110000XXXXXXXX", price=128.50, used=False, category="餐饮费" ) invoice.save() # 查询未使用的发票 unused_invoices = Invoice.objects.filter(used=False) # 按类型统计发票金额 from django.db.models import Sum total_dining = Invoice.objects.filter( category="餐饮费", used=False ).aggregate(Sum('price')) print(f"餐饮费总额: {total_dining['price__sum']}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.