How to Read and get Texts from Docx Files in WordPress |
X

Congrats, You are Subscribed to Receive Updates.

How to Read and get Texts from Docx Files in WordPress


How to Read and get Texts from Docx Files in WordPress. I think there is no plugin helps you to process Docx files and get text contents from the file. So I tried to get a ready made code from different web forums and blogs. But i couldn’t get one. So i decided to go with my own code. Than find some informative things and made myself one.  Here is our code to go with live workings. Let’s start with php code.

<?php
add_action('kv_read_docx' , 'kv_read_word_wordpress', 10, 1);

function kv_read_word_wordpress($input_file){	
	 $kv_strip_texts = ''; 
         $kv_texts = ''; 	
	if(!$input_file || !file_exists($input_file)) return false;
		
	$zip = zip_open($input_file);
		
	if (!$zip || is_numeric($zip)) return false;	
	
	while ($zip_entry = zip_read($zip)) {
			
		if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
			
		if (zip_entry_name($zip_entry) != "word/document.xml") continue;

		$kv_texts .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
			
		zip_entry_close($zip_entry);
	}	
	zip_close($zip);		

	$kv_texts = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $kv_texts);
	$kv_texts = str_replace('</w:r></w:p>', "\r\n", $kv_texts);
	$kv_strip_texts = strip_tags($kv_texts);
	
        if($kv_strip_texts!== false) {		
	      echo nl2br($kv_strip_texts);	
        } else {
	     echo 'Can't Read that file.';
        }
}
?>

The above one creates an action hook for reading function. And now we need to provide the docx file with absolute path. than it will be parsed into the wordpress hooks.

<?php
    $file_url = 'path/to/the/file/kvcodes.docx'; 

    do_action('kv_read_docx', $file_url);

?>

Here the above example i just processed the Docx file and print as since as it is. but you can use php string functions to get particular components. Lets take an example, if we are processing an quotation or something like that. Usually all the quotations begins with a date and place. Here we gonna read the date.

if($kv_strip_texts!== false) {		
	     $kv_string_pos =  stripos($kv_strip_texts, 'date ');
	     echo $kv_Date = substr($kv_strip_texts, $kv_string_pos); 

        } else {
	     echo 'Can't Read that file.';
        }

The above snippet prints the date. Likewise you can also find particular string in it. I wrote an another article for the php users, who use it for their php web projects.

 How to read and view docx Files using PHP.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar