Here is a simple example of how you can create a React app to upload a PowerPoint (PPT or PPTX) file and display its contents in a textbox. To accomplish this, we will utilize the pptx-parser library to read the content of the PowerPoint file.
First, ensure you have the required packages installed:
npx create-react-app ppt-upload-app
cd ppt-upload-app
npm install pptx-parser
npm start
Next, update the src/App.js file with the following code:
import React, { useState } from 'react';
import { readFileSync } from 'fs';
import pptxParser from 'pptx-parser';
function App() {
const [content, setContent] = useState('');
const handleFileUpload = async (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = async (event) => {
const buffer = event.target.result;
try {
const pptContent = await pptxParser(buffer);
let textContent = '';
pptContent.slides.forEach(slide => {
slide.texts.forEach(text => {
textContent += text.text + ' ';
});
textContent += `\n==== Slide ${slide.index + 1} End ====\n`;
});
setContent(textContent);
} catch (error) {
console.error('Error parsing PPTX file:', error);
setContent('Error parsing PPTX file');
}
};
reader.readAsArrayBuffer(file);
}
};
return (
<div className="App">
<header className="App-header">
<h1>Upload PPT File</h1>
<input type="file" accept=".ppt,.pptx" onChange={handleFileUpload} />
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
rows="20"
cols="80"
readOnly
/>
</header>
</div>
);
}
export default App;
This example sets up a React component that can handle file uploads and extract text content from a PowerPoint file. Here's a quick summary:
- File Input: An input field to upload
.ppt or .pptx files.
- FileReader: To read the uploaded file as a binary array buffer.
- pptx-parser: To parse the PPTX file contents and extract text.
- TextArea: To display the extracted content.
Ensure you have the library installed and properly set up your environment. If you face any issues with parsing, some additional configurations may be required for large files or specific content within PowerPoint slides.
Remember, this is a basic example and might not cover edge cases or large PowerPoint files efficiently. For more advanced parsing and handling, you might need to delve deeper into processing libraries or APIs specialized in handling PowerPoint files.