【问题标题】:Ignore images when converting word document to text using Interop in C#在 C# 中使用 Interop 将 word 文档转换为文本时忽略图像
【发布时间】:2020-06-04 20:35:32
【问题描述】:

目前,我的代码成功地将 word 文档 (.docx) 中的所有文本转换为 .txt 文件中的纯文本,但无论 word doc 中有图像,它都会在我的输出文件中被替换为“/”。如何忽略图像?

我的代码存根:

Word.Application app = new Word.Application();
Word.Document doc;
object missing = Type.Missing;
object readOnly = true;
doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
string text = doc.Content.Text;
System.IO.File.WriteAllText(txtPath, text);
Console.WriteLine("File converted to .txt!");

【问题讨论】:

  • 嗯,我不确定 Content 对象是否有能力做到这一点。读出文本时可能只需要替换这些字符。字符串文本 = doc.Content.Text.Replace("'/'","");或者您是否明确需要忽略最初从内容读取的图像?
  • 如果我使用替换方法,我可能会丢失原始 word 文件中的任何“/”字符,所以我认为我最好的选择是在初始读取时忽略...除非有更好的方法。
  • 啊,好吧,我正在阅读,因为图像被替换“包括”正斜杠周围的单引号,我认为这种情况很少见。
  • 这些ref missing在你的代码中做了什么?

标签: c# office-interop docx


【解决方案1】:

在访问内容之前删除所有图像怎么样?

类似这样的:

while (doc.InlineShapes.Count > 0)
{
    doc.InlineShapes(1).Delete();  //Collection is 1-based, first element is 1, not 0; at least when using it within VBA (weird language...)

}
// and with Shapes as well
while (doc.Shapes.Count > 0)
{
    doc.Shapes(1).Delete();
}

string text = doc.Content.Text;

【讨论】:

    【解决方案2】:

    一种不同的方法,而不是我上面建议的方法

    只需将文档另存为文本

    object path = txtPath; 
    const int wdFormatText = 2;
    object fileFormat = wdFormatText;
    doc.SaveAs (ref path, ref fileFormat, ref missing, ...) // other missing parameter
    

    还有一个方法SaveAs2,如果您继续传递missing 以获得更多参数,我假设它会是相同的

    【讨论】:

      【解决方案3】:

      这是我的解决方案。此类读取 Word 文档,删除所有图像,然后将其转换为 RTF 文件。

      using Microsoft.Office.Interop.Word;
      using System.IO;
      using OW = Microsoft.Office.Interop.Word;
      
      namespace WordImagesCruncher
      {
          public class WordImagesCruncher
          {
              public string SourceFilePath { private set; get; }
      
              public WordImagesCruncher(string sourceFilePath)
              {
                  SourceFilePath = sourceFilePath;
              }
      
              public void DoWork()
              { 
                  var wordApp = new OW.Application();
                  OW.Document doc = wordApp.Documents.Open(SourceFilePath);
      
                  for (int i = doc.InlineShapes.Count; i > 0; i--)
                  {
                      doc.InlineShapes[1].Delete();
                  } 
      
                  for (int i = doc.Shapes.Count; i >0 ; i--)
                  {
                      doc.Shapes[1].Delete();
                  }
      
                  doc.SaveAs(Path.GetFileNameWithoutExtension(SourceFilePath) + "_tmp.rtf", OW.WdSaveFormat.wdFormatRTF);
                  doc.Close();
                  wordApp.Quit();
              }
          }
      }
      

      别忘了添加互操作参考

      并将属性Embed Interop Types 更改为Yes。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-22
        • 2022-11-30
        • 2011-06-19
        • 2015-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多