doc、docx、txt间格式转换的.vbs代码

'  ** 根据脚本的名称进行对应转换, 如可将脚本命名为:Word2Txt.vbs, Word2Doc.vbs,  Word2Docx.vbs, Word2Html.vbs, 如为其它脚本名称则转换为 .txt**

' * =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = *
' * 批量 Word 格式转换工具,   BY: hongqingfa                        2023-09-10 *
' * =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = *
wdFormatDocument97=0	'Microsoft Word 97 document format.
wdFormatDocument=0		'Microsoft Office Word 97 - 2003 binary file format.
wdFormatDOSText=4		'Microsoft DOS text format.
wdFormatRTF=6			'Rich text format (RTF).
wdOpenFormatWebPages=7	'HTML 格式。
wdFormatXMLDocument=12	'XML document format.
wdFormatOpenDocumentText=23	'OpenDocument Text format.
'
'wdOpenFormatAuto 0 现有格式。
'wdOpenFormatDocument 1 Word 格式。
'wdOpenFormatDocument97 1 Microsoft Word 97 文档格式。
'wdOpenFormatTemplate97 2 Word 97 模板格式。
'wdOpenFormatTemplate 2 用作 Word 模板。
'wdOpenFormatRTF 3 RTF 格式。
'wdOpenFormatText 4 未编码的文本格式。
'wdOpenFormatEncodedText 5 编码文本格式。
'wdOpenFormatUnicodeText 5 Unicode 文本格式。
'wdOpenFormatWebPages 7 HTML 格式。
'wdOpenFormatXML 8 XML 格式。
'wdOpenFormatXMLDocument 9 XML 文档格式。
'wdOpenFormatXMLDocumentMacroEnabled 10 启用了宏的 XML 文档格式。
'wdOpenFormatXMLTemplate 11 XML 模板格式。
'wdOpenFormatXMLTemplateMacroEnabled 12 启用了宏的 XML 模板格式。
'wdOpenFormatAllWordTemplates 13 Word 模板格式。
'
' * =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = *
'
Set objArgs = WScript.Arguments
If objArgs.Count=0 Then
    MsgBox "本脚本 支持 .doc/.docx/.txt/.html 间的转换," &vbCrLf& "请将您要转换的文件拖到 本脚本!" &vbCrLf& "" &vbCrLf& "拖拽批量 Word 格式转换工具, BY: HongQingFa"
    MsgBox "根据脚本的名称进行对应转换," &vbCrLf& "如可将脚本命名为:" &vbCrLf& "Word2Txt.vbs"  &vbCrLf& "Word2Doc.vbs"  &vbCrLf& "Word2Docx.vbs"  &vbCrLf&  "Word2Html.vbs"  &vbCrLf& "-------"   &vbCrLf& "-其它脚本名称-,则转换为 .txt" 
End If
'
' 根据本脚本的名称进行对应转换,其它命名格式则转换为 .txt
scp_fn=UCase(wscript.scriptname)
For I = 0 To objArgs.Count - 1
    FileUrl = objArgs(I)

'	根据脚本的名称进行对应转换,其它命名格式则转换为 .txt
'	Call Word2Txt(FileUrl)
	if scp_fn = UCase("Word2Txt.vbs") Then
		Call Word2Txt(FileUrl)
	elseif  scp_fn = UCase("Word2Doc.vbs") Then
		Call Word2Doc(FileUrl)
	elseif scp_fn = UCase("Word2Docx.vbs") Then
		Call Word2Docx(FileUrl)
	elseif  scp_fn = UCase("Word2Html.vbs") Then
		Call Word2Html(FileUrl)
	else
		Call Word2Txt(FileUrl)
	End If
Next

Function Word2Html(FileUrl)
   FilePath = GetFileBaseName(FileUrl) & ".html"
   Set objWord = CreateObject("Word.Application")
   Set wordapp = objWord.Documents.Open(FileUrl, False, True)
   wordapp.SaveAs FilePath, wdOpenFormatWebPages
   wordapp.Close True
End Function

Function Word2Doc(FileUrl)
   FilePath = GetFileBaseName(FileUrl) & ".doc"
   Set objWord = CreateObject("Word.Application")
   Set wordapp = objWord.Documents.Open(FileUrl, False, True)
   wordapp.SaveAs FilePath, wdFormatDocument97
   wordapp.Close True
End Function

Function Word2Docx(FileUrl)
   FilePath = GetFileBaseName(FileUrl) & ".docx"
   Set objWord = CreateObject("Word.Application")
   Set wordapp = objWord.Documents.Open(FileUrl, False, True)
   wordapp.SaveAs FilePath, wdFormatXMLDocument
   wordapp.Close True
End Function

Function Word2Txt(FileUrl)
   FilePath = GetFileBaseName(FileUrl) & ".txt"
   Set objWord = CreateObject("Word.Application")
   Set wordapp = objWord.Documents.Open(FileUrl, False, True)
   wordapp.SaveAs FilePath, wdFormatDOSText
   wordapp.Close True
End Function

' 取得文件基本名.
Function GetFileBaseName(ByVal sfilename)
  n = InStrRev(sfilename, ".")
  If n>1 Then
    GetFileBaseName = Left(sfilename, n-1)
  Else
    GetFileBaseName = sfilename
  End If
End Function
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现基于Java合并docdocx格式的Word文件,可以考虑使用Apache POI库来操作Word文件。下面是一个简单的示例代码,可以将多个docx文件合并为一个: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.xwpf.usermodel.*; public class MergeWordFiles { public static void main(String[] args) throws IOException { String[] filesToMerge = {"file1.docx", "file2.docx", "file3.docx"}; String mergedFileName = "merged.docx"; List<XWPFDocument> files = new ArrayList<>(); for (String fileName : filesToMerge) { XWPFDocument document = new XWPFDocument(OPCPackage.open(fileName)); files.add(document); } XWPFDocument mergedDocument = mergeDocuments(files); FileOutputStream outputStream = new FileOutputStream(new File(mergedFileName)); mergedDocument.write(outputStream); outputStream.close(); } private static XWPFDocument mergeDocuments(List<XWPFDocument> files) { XWPFDocument mergedDocument = new XWPFDocument(); XWPFParagraph newParagraph; for (XWPFDocument document : files) { for (XWPFParagraph paragraph : document.getParagraphs()) { newParagraph = mergedDocument.createParagraph(); newParagraph.setAlignment(paragraph.getAlignment()); for (XWPFRun run : paragraph.getRuns()) { newParagraph.createRun().setText(run.getText(0)); } } for (XWPFTable table : document.getTables()) { mergedDocument.createTable().addNewCol(); mergedDocument.createTable().addNewRow(); for (XWPFTableRow row : table.getRows()) { XWPFTableRow newRow = mergedDocument.createTable().getRow(0); newRow.setHeight(row.getHeight()); for (XWPFTableCell cell : row.getTableCells()) { XWPFTableCell newCell = newRow.addNewTableCell(); newCell.setVerticalAlignment(cell.getVerticalAlignment()); newCell.setAlignment(cell.getAlignment()); newCell.setText(cell.getText()); } } } } return mergedDocument; } } ``` 这个示例代码中,先将要合并的docx文件读取为XWPFDocument对象,然后将所有文件合并到一个新的XWPFDocument对象中,最后将合并后的XWPFDocument对象写入到一个新的docx文件中。 需要注意的是,这个示例代码只能合并docx格式的Word文件,如果需要合并doc格式的Word文件,需要使用HWPF库进行操作。同时,如果要处理较大的Word文件,可能需要考虑分段处理,以避免内存溢出等问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值