-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to read docx file into the database(Mysql) #1192
Comments
If I understand correctly, you want to fetch "PlaceholderText" to get "单击或点击此处输入文字。</" and in the timedate pulldown menu, you want the value of the date (2017-11-11T00:00:00Z), get the datatype (dateTime) maybe the fieldname of the table (time) ? <w:sdtPr>
<w:alias w:val="time"/>
<w:date w:fullDate="2017-11-11T00:00:00Z">
<w:dateFormat w:val="yyyy/M/d"/>
<w:lid w:val="zh-CN"/>
<w:storeMappedDataAs w:val="dateTime"/>
<w:calendar w:val="gregorian"/>
</w:date>
</w:sdtPr> This works with my fork of TemplateProcessor. It searches for something, like "CASE 2" (but consider using bookmarks or something else that allows for a better find). Then, once it find the paragraph w:p around "CASE 2", it will look for w:sdtPr to the right of this paragraph, and return that xml segment, which we convert to a structure so we can access its values. (which needs work as xml tags can shift, and not always be in You need to replace the original template processor file with my TemplateProcessor.php $file_dir = storage_path('phpwordtomysql.docx');
$template = new \PhpOffice\PhpWord\TemplateProcessor($file_dir);
$xmldata = $template->processSegment('CASE 2', 'w:p', \PhpOffice\PhpWord\TemplateProcessor::SEARCH_AROUND, 1, 'MainPart', function(&$xmlSegment, &$segmentStart, &$segmentEnd, &$part){
$segmentStart = strpos($part, '<w:sdtPr', $segmentEnd + 1);
if (!$segmentStart) dd("FATAL: nothing found!");
$segmentEnd = strpos($part, '</w:sdtPr>', $segmentStart + 8);
$xmlSegment = substr($part, $segmentStart, ($segmentEnd - $segmentStart));
return false; # only getSegment
});
$p = xml_parser_create();
xml_parse_into_struct($p, $xmldata, $vals, $index);
xml_parser_free($p);
$date = $vals[2]{'attributes'}{'W:FULLDATE'}; # "date" = "2017-11-11T00:00:00Z"
$type = $vals[5]{'attributes'}{'W:VAL'}; # "type" = "dateTime"
$hash = [];
array_walk($vals, function(&$item) use(&$hash){
if(array_key_exists('attributes',$item) && array_key_exists('tag',$item)){
$hash[ $item['tag'] ] = array_values($item['attributes'])[0] ;
}
});
dd(compact('vals','index','date', 'type', 'hash')); *note: dd() is Laravel's die-and-dump. This function may not be available for you, replace it with var_dump() or similar this will return:
|
Why this function processSegment return false and Warning on line 602 in your TemplateProcessor.php? Could you help me solve the problem ? @FBnil |
@zw12579 I recently added
|
@FBnil Thank you very much, I try to correct it! |
@zw12579 And I updated my source yet again, so you will need to download TemplateProcessor again (a bug introduced when I added search direction). |
@FBnil What should I do if I want to get a picture from a docx file?Now,The picture in the xml file is a connection.This is my example code. Thanks!
|
You will have to expose the zipclass and use getFromName, modify TemplateProcessor to also have this function: /**
* Retrieve a file inside the document (possibly binary data)
*
* @param string $localname
*
* @return string content of the file
*/
public function getFromName($localname)
{
return $this->zipClass->getFromName($localname);
} Now all you need to figure out is which In the relations ( <Relationship Id="rId2" Type="https://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/>
<Relationship Id="rId3" Type="https://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.png"/> So you need to use getFromName to get the content of document.xml.rels; then use |
You helped me a lot, once again thank you! |
I want to put with a control element of the word document, in accordance with the control name or type written into the database(Mysql), do not know what method to achieve, I am also concerned about the phpword, don't know if I can do it, hoping to provide methods, thank you. Ps.I offer a control element word document examples.
phpwordtomysql.docx
The text was updated successfully, but these errors were encountered: