[英]I have a word doc. i want to get word count per page of word doc?
我只能找到每行的解決方案,但找不到分頁符; 也很困惑。 對於docx也無法找到確切的字數。
function read_doc($filename) {
$fileHandle = fopen($filename, "r");
$line = @fread($fileHandle, filesize($filename));
$lines = explode(chr(0x0D), $line);
$outtext = "";
foreach ($lines as $key => $thisline) {
if( $key > 11 ){
var_dump($thisline);
$pos = strpos($thisline, chr(0x00));
if (($pos !== FALSE) || (strlen($thisline) == 0)) {
continue;
} else {
var_dump($thisline);
$text = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/", "", $thisline);
var_dump($text);
}
}
}
return $outtext;
}
為此實現自己的代碼聽起來不是一個好主意。 我建議使用PHPWord等外部庫。 它應該允許您將文件轉換為純文本。 然后,您可以從中提取單詞計數。
此外,諸如此類的外部庫增加了對許多文件格式的支持,而不是限制您使用Word 97-2003。
這是一個VB.NET代碼的基本部分,它計算每頁的單詞數,但要注意它取決於Word認為是單詞的內容,它不一定是用戶認為單詞的內容。 根據我的經驗,您需要正確分析Word的行為方式,解釋的內容,然后構建邏輯以確保獲得所需的結果。 它不是PHP,但它可以完成工作並且可以成為您的起點。
Structure WordsPerPage
Public pagenum As String
Public count As Long
End Structure
Public Sub CountWordsPerPage(doc As Document)
Dim index As Integer
Dim pagenum As Integer
Dim newItem As WordsPerPage
Dim tmpList As New List(Of WordsPerPage)
Try
For Each wrd As Range In doc.Words
pagenum = wrd.Information(WdInformation.wdActiveEndPageNumber)
Debug.Print("Word {0} is on page {1}", wrd.Text, pagenum)
index = tmpList.FindIndex(Function(value As WordsPerPage)
Return value.pagenum = pagenum
End Function)
If index <> -1 Then
tmpList(index) = New WordsPerPage With {.pagenum = pagenum, .count = tmpList(index).count + 1}
Else
' Unique (or first)
newItem.count = 1
newItem.pagenum = pagenum
tmpList.Add(newItem)
End If
Next
Catch ex As Exception
WorkerErrorLog.AddLog(ex, Err.Number & " " & Err.Description)
Finally
Dim totalWordCount As Long = 0
For Each item In tmpList
totalWordCount = totalWordCount + item.count
Debug.Print("Page {0} has {1} words", item.pagenum, item.count)
Next
Debug.Print("Total word count is {0}", totalWordCount)
End Try
End Sub
解壓縮.doc或.docx文件時,您將獲得文件夾。 在word子文件夾中查找document.xml文件。 您將獲得包含xml語法的完整文檔。 按頁xml語法拆分字符串, 刪除 xml語法並使用str_word_count 。
什么是我需要一個Windows服務器: - 使用COM對象;; 請檢查此鏈接https://github.com/lettertoamit/MS-Word-PER-PAGE-WORDCOUNT/blob/master/index.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.