How to Convert DOCX to TXT Text in PHP

Cloudmersive
1 min readFeb 9, 2023

--

Despite DOCX’s widespread popularity, plain, unformatted text still has a much broader use-case than DOCX formatted text. Using the below API in your PHP application, you can easily convert incoming DOCX files to plain text (TXT) format and specify how whitespace should be handled in that conversion. To take advantage of this API, simply follow steps to structure your API call using ready-to-run PHP code examples.

First, run the below command to install the PHP SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Right after that, copy in the below code to capture your API key and call the function:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$text_formatting_mode = "text_formatting_mode_example"; // string | Optional; specify how whitespace should be handled when converting the document to text. Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases. Default is 'minimizeWhitespace'.

try {
$result = $apiInstance->convertDocumentDocxToTxt($input_file, $text_formatting_mode);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentDocxToTxt: ', $e->getMessage(), PHP_EOL;
}
?>

To get an API key at zero cost, register a free account on our website. This will provide you with a limit of 800 API calls per month & no hidden financial commitments.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet