Что нового

[Данные, строки] [Word] Чтение документа ".docx" выдавая требуемые колонки из таблицы

MnM

Post-Hardcore
Сообщения
679
Репутация
90
Версия AutoIt: 3.3.8.1

Описание:
Парсинг\чтение документа docx в котором есть таблица(возможно две), требуется получить из ячеек определенного столбца данные. Столбцами являются группы.
Примечание:
Выложу примерный документ - s.docx
Думаю будет понятно что для каждой группы хотелось бы получить намеченные ей уроки :smile:
 

WSWR

AutoIT Гуру
Сообщения
941
Репутация
363
MnM
Как-то так можно:
Разархивируем из docx с помощью функции из _zip.au3, читаем xml
Там табличка, к сожалению, с дополнительной ячейкой в одно строке, по-простому не получается свести в 2D-массив, поэтому получаем одномерный массив, а дальше можно еще как-нибудь преобразовать. В принципе, читаемо)

Код:
#include <_zip.au3> ; http://www.autoitscript.com/forum/topic/116565-zip-udf-zipfldrdll-library/?p=813281
#include <Array.au3>

$sPath = @ScriptDir & '\s.docx'
$desPath = @ScriptDir & '\docx_zip'
FileCopy($sPath, $desPath & '\s.zip', 9)
_Zip_UnzipAll($desPath & '\s.zip', $desPath)


$sText = FileRead($desPath & '\word\document.xml')
$sText = StringReplace($sText, ' ', '')
$aText = StringSplit($sText, 'w:w="', 1)
$sTring = ''
$sTring1 = ''
$x = 0

For $i = 1 To $aText[0]

	$aString = StringRegExp($aText[$i], '<w:t>(.*?)</w:t>|serve">(.*?)</w:t>', 3)
	$sTring = _ArrayToString($aString, '')
	StringReplace($sTring, '-', '')
	If @extended = 2 And $x = 0 Then $x = 1

	If $sTring = '0' Then $sTring = 'X000'

	If $x = 1 Then
		If $sTring = '' Then
			$sTring1 &= $sTring & '---------|'
		Else
			$sTring1 &= $sTring & '|'
		EndIf
	EndIf
Next

$aText = _ArrayClearEmpty1(StringSplit('Время         |' & $sTring1, 'IVX'))

_ArrayDisplay($aText)


Func _ArrayClearEmpty1($a_Array, $i_SubItem = 0, $i_Start = 0)
	If Not IsArray($a_Array) Or UBound($a_Array, 0) > 2 Then Return SetError(1, 0, 0)

	Local $i_Index = -1
	Local $i_UBound_Row = UBound($a_Array, 1) - 1
	Local $i_UBound_Column = UBound($a_Array, 2) - 1

	If $i_UBound_Column = -1 Then $i_UBound_Column = 0
	If $i_SubItem > $i_UBound_Column Then $i_SubItem = 0
	If $i_Start < 0 Or $i_Start > $i_UBound_Row Then $i_Start = 0

	Switch $i_UBound_Column + 1
		Case 1
			Dim $a_TempArray[$i_UBound_Row + 1]
			If $i_Start Then
				For $i = 0 To $i_Start - 1
					$a_TempArray[$i] = $a_Array[$i]
				Next
				$i_Index = $i_Start - 1
			EndIf
			For $i = $i_Start To $i_UBound_Row
				If String($a_Array[$i]) Then
					$i_Index += 1
					$a_TempArray[$i_Index] = $a_Array[$i]
				EndIf
			Next
			If $i_Index > -1 Then
				ReDim $a_TempArray[$i_Index + 1]
			Else
				Return SetError(2, 0, 0)
			EndIf
		Case 2
			Dim $a_TempArray[$i_UBound_Row + 1][$i_UBound_Column + 1]
			If $i_Start Then
				For $i = 0 To $i_Start - 1
					For $j = 0 To $i_UBound_Column
						$a_TempArray[$i][$j] = $a_Array[$i][$j]
					Next
				Next
				$i_Index = $i_Start - 1
			EndIf
			For $i = $i_Start To $i_UBound_Row
				If String($a_Array[$i][$i_SubItem]) Then
					$i_Index += 1
					For $j = 0 To $i_UBound_Column
						$a_TempArray[$i_Index][$j] = $a_Array[$i][$j]
					Next
				EndIf
			Next
			If $i_Index > -1 Then
				ReDim $a_TempArray[$i_Index + 1][$i_UBound_Column + 1]
			Else
				Return SetError(2, 0, 0)
			EndIf
	EndSwitch
	Return SetError(0, $i_UBound_Row - $i_Index, $a_TempArray)
EndFunc   ;==>_ArrayClearEmpty1
 
Автор
MnM

MnM

Post-Hardcore
Сообщения
679
Репутация
90
WSWR
Спасибо большое! :smile:
Дальше попробую сам сделать там уже с дефисами легче=)
 
Верх