Flutter PowerPoint解析插件dart_pptx的使用

Flutter PowerPoint解析插件dart_pptx的使用

dart_pptx 是一个纯 Dart 库,可以使用 Dart 类或 Markdown 创建 PowerPoint 演示文稿。该库可以在原生、Web 和移动应用程序中使用。

开始使用

要使用此库,请在 pubspec.yaml 文件中添加 dart_pptx 作为依赖项:

flutter pub add dart_pptx

使用方法

创建演示文稿

首先,创建一个新的演示文稿对象:

import 'package:dart_pptx/dart_pptx.dart';

final pres = Powerpoint();
保存演示文稿

要保存演示文稿,调用 save 方法:

final bytes = await pres.save();

然后可以将字节保存到文件中:

import 'dart:io';

final file = File('my_presentation.pptx');
await file.writeAsBytes(bytes);
添加幻灯片

所有参数都是可选的,如果不提供,将显示“双击编辑”占位符文本。

标题幻灯片
pres.addTitleSlide(
    title: 'Title'.toTextValue(),
    author: 'Author'.toTextValue(),
);
标题和图片幻灯片
pres.addTitleAndPhotoSlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
    author: 'Author'.toTextValue(),
    image: ImageReference(
        path: './samples/images/sample_gif.gif',
        name: 'Sample Gif',
    ),
);
标题和图片幻灯片(替代)
pres.addTitleAndPhotoAltSlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
    image: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
);
标题和项目符号幻灯片
pres.addTitleAndBulletsSlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
    bullets: [
        'Bullet 1',
        'Bullet 2',
        'Bullet 3',
        'Bullet 4',
    ].map((e) => e.toTextValue()).toList(),
);
项目符号幻灯片
pres.addBulletsSlide(
    bullets: [
        'Bullet 1',
        'Bullet 2',
        'Bullet 3',
        'Bullet 4',
    ].map((e) => e.toTextValue()).toList(),
);
标题、项目符号和图片幻灯片
pres.addTitleBulletsAndPhotoSlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
    bullets: [
        'Bullet 1',
        'Bullet 2',
        'Bullet 3',
        'Bullet 4',
    ].map((e) => e.toTextValue()).toList(),
    image: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
);
分段幻灯片
pres.addSectionSlide(
    section: 'Section'.toTextValue(),
);
仅标题幻灯片
pres.addTitleOnlySlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
);
议程幻灯片
pres.addAgendaSlide(
    title: 'Title'.toTextValue(),
    subtitle: 'Subtitle'.toTextValue(),
    topics: 'Topics'.toTextValue(),
);
陈述幻灯片
pres.addStatementSlide(
    statement: 'Statement'.toTextValue(),
);
大事实幻灯片
pres.addBigFactSlide(
    information: 'Information'.toTextValue(),
    fact: 'Fact'.toTextLine(),
);
引用幻灯片
pres.addQuoteSlide(
    quote: 'Quote'.toTextLine(),
    attribution: 'Attribution'.toTextValue(),
);
三张图片幻灯片
pres.addPhoto3UpSlide(
    image1: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
    image2: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
    image3: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
);
图片幻灯片
pres.addPhotoSlide(
    image: ImageReference(
        path: './samples/images/sample_jpg.jpg',
        name: 'Sample Jpg',
    ),
);
空白幻灯片
pres.addBlankSlide();
使用 Markdown 创建幻灯片

也可以使用 Markdown 创建幻灯片:

await pres.addSlidesFromMarkdown('MARKDOWN SOURCE');

Markdown 格式遵循 md2googleslides 的格式。

幻灯片背景
实心颜色

颜色需要以十六进制格式表示,且不包含前导 #

slide.background.color = 'FF0000';
图片
slide.background.image = ImageReference(
    path: './samples/images/sample_jpg.jpg',
    name: 'Sample Jpg',
);
讲者备注
slide.speakerNotes = 'Notes'.toTextValue();
显示幻灯片编号
slide.showSlideNumber = true;

或者为整个演示文稿设置:

pres.showSlideNumbers = true;
图片

图片使用 ImageReference 类。path 可以是 base64 编码字符串、远程 URL 或本地文件路径。

名称用于 alt 属性,可以用描述覆盖。

当演示文稿保存时,所有图片将被下载并保存在演示文稿中。

在 Flutter Web 上使用远程 URL 时,如果服务器没有正确的 CORS 头,可能会导致 CORS 错误。可以通过使用代理服务器来解决此问题。

布局
4x3
pres.layout = Layout.screen4x3();
16x9
pres.layout = Layout.screen16x9();
16x10
pres.layout = Layout.screen16x10();
宽屏
pres.layout = Layout.screenWide();
自定义
pres.layout = Layout(
    type: 'custom',
    width: 24384000,
    height: 13716000,
);
元数据
标题
pres.title = 'Title';
主题
pres.subject = 'Subject';
作者
pres.author = 'Author';
公司
pres.company = 'Company';
修订
pres.revision = 'Revision';

完整示例 Demo

以下是一个完整的示例代码,展示了如何使用 dart_pptx 创建一个简单的 PowerPoint 演示文稿:

import 'package:dart_pptx/dart_pptx.dart';
import 'dart:io';

void main() async {
  // 创建一个新的演示文稿
  var pres = PowerPoint();

  // 添加标题幻灯片
  pres.addTitleSlide(
    title: TextValue.uniform('Hello World'),
    author: TextValue.uniform('Author Name'),
  );

  // 添加带图片的标题幻灯片
  pres.addTitleAndPhotoSlide(
    title: TextValue.uniform('Title and Photo Slide'),
    subtitle: TextValue.uniform('Subtitle'),
    author: TextValue.uniform('Author Name'),
    image: ImageReference(
      path: './samples/images/sample_image.jpg',
      name: 'Sample Image',
    ),
  );

  // 添加带项目符号的标题幻灯片
  pres.addTitleAndBulletsSlide(
    title: TextValue.uniform('Title and Bullets Slide'),
    subtitle: TextValue.uniform('Subtitle'),
    bullets: [
      'Bullet 1',
      'Bullet 2',
      'Bullet 3',
      'Bullet 4',
    ].map((e) => TextValue.uniform(e)).toList(),
  );

  // 添加空白幻灯片
  pres.addBlankSlide();

  // 设置幻灯片布局为 16x9
  pres.layout = Layout.screen16x9();

  // 设置演示文稿元数据
  pres.title = 'My Presentation';
  pres.subject = 'Presentation Subject';
  pres.author = 'Author Name';
  pres.company = 'Company Name';
  pres.revision = '1.0';

  // 保存演示文稿
  final bytes = await pres.save();
  if (bytes != null) {
    // 将字节保存到文件
    File('./hello.pptx').writeAsBytesSync(bytes);
    print('Presentation saved successfully');
  } else {
    print('Failed to save presentation');
  }
}
1 回复

更多关于Flutter PowerPoint解析插件dart_pptx的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用dart_pptx插件来解析PowerPoint文件(PPTX)的示例代码。dart_pptx是一个Dart库,用于读取和写入PowerPoint文件,但它目前主要是读取功能更为完善。在Flutter中使用它时,通常是通过Dart代码来进行文件处理,然后在Flutter UI中展示结果。

首先,确保你已经在pubspec.yaml文件中添加了dart_pptx依赖:

dependencies:
  flutter:
    sdk: flutter
  dart_pptx: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

以下是一个简单的示例,展示如何读取一个PPTX文件并获取其中的幻灯片标题:

import 'package:flutter/material.dart';
import 'package:dart_pptx/dart_pptx.dart';
import 'dart:io';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PPTX Parser Example'),
        ),
        body: Center(
          child: PptxParserExample(),
        ),
      ),
    );
  }
}

class PptxParserExample extends StatefulWidget {
  @override
  _PptxParserExampleState createState() => _PptxParserExampleState();
}

class _PptxParserExampleState extends State<PptxParserExample> {
  List<String> slideTitles = [];

  @override
  void initState() {
    super.initState();
    _parsePptx();
  }

  Future<void> _parsePptx() async {
    // 假设PPTX文件位于应用的文档目录中
    final file = File('path/to/your/presentation.pptx');

    // 读取文件内容
    final Uint8List fileBytes = await file.readAsBytes();

    // 使用dart_pptx解析PPTX
    final presentation = PresentationParser().parse(fileBytes);

    // 提取每个幻灯片的标题(这里假设标题在第一个形状中)
    for (final slide in presentation.slides) {
      for (final shape in slide.shapes) {
        if (shape is TextShape) {
          final textShape = shape as TextShape;
          if (textShape.textFrame.textRuns.isNotEmpty) {
            final title = textShape.textFrame.textRuns.first.text;
            setState(() {
              slideTitles.add(title);
            });
            break;  // 假设每个幻灯片只有一个标题,找到后退出
          }
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('Slide Titles:'),
        ...slideTitles.map((title) => Text(title)),
      ],
    );
  }
}

注意

  1. path/to/your/presentation.pptx需要替换为你的PPTX文件的实际路径。在Flutter中,你可能需要使用getApplicationDocumentsDirectory()来获取应用的文档目录路径,然后将PPTX文件放置在那里或者从其他位置复制过去。
  2. 上述代码仅展示了如何提取每个幻灯片的第一个文本形状作为标题。实际的PPTX文件可能包含更复杂的结构和内容,你需要根据具体需求进行解析和处理。
  3. dart_pptx库可能还在不断更新和完善中,请参考其官方文档(如果有的话)或GitHub仓库以获取最新信息和功能。

希望这个示例能帮你开始使用dart_pptx来解析PPTX文件!

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!