Convert string to word

I’m trying to essentially take a list of strings containg sentences such as:

sentence = ['Here is an example of what I am working with', 'But I need to change the format', 'to something more useable']

and convert it into the following:

word_list = ['Here', 'is', 'an', 'example', 'of', 'what', 'I', 'am',
'working', 'with', 'But', 'I', 'need', 'to', 'change', 'the format',
'to', 'something', 'more', 'useable']

I tried using this:

for item in sentence:
    for word in item:
        word_list.append(word)

I thought it would take each string and append each item of that string to word_list, however the output is something along the lines of:

word_list = ['H', 'e', 'r', 'e', ' ', 'i', 's' .....etc]

I know I am making a stupid mistake but I can’t figure out why, can anyone help?

Skip to content

In this article, I am going to demonstrate two approaches to convert HTML to Word in Java applications, they are:

  • Convert HTML to Word
  • Convert HTML String to Word

Add Dependencies

Free Spire.Doc for Java library is used to implement this task. If you use maven, you need to specify the following dependencies in your project’s pom.xml file for including Free Spire.Doc for Java into your Java project.

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.doc.free</artifactId>    
        <version>3.9.0</version>    
    </dependency>    
</dependencies> 

For non-maven projects, you can download Free Spire.Doc for Java pack from this website and add Spire.Doc.jar in the lib folder into your project as a dependency.

Convert HTML to Word

Free Spire.Doc for Java library provides a Document class that represents a Word document. This class offers a saveToFile(String, FileFormat) method that can be used to convert a HTML file to Word document.

The input HTML document:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.XHTMLValidationType;

public class HtmlToWord {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Load a Html file
        document.loadFromFile("Input.html", FileFormat.Html, XHTMLValidationType.None);

        //Save Html to Word
        document.saveToFile("HtmlToWord.docx",FileFormat.Docx_2013);
    }
}

The output Word document:

Convert HTML String to Word

To convert a HTML String to Word, you need to invoke the Paragraph.appendHTML(String) method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;

public class HtmlStringToWord {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Add a section
        Section sec = document.addSection();

        //HTML string
        String htmlString = "<html><head/><body> <h1>Html Heading</h1><p>This is an html document in a string literal.</p></body></html>";

        //Add a paragraph to the section and append a html string to the paragraph
        sec.addParagraph().appendHTML(htmlString);

        //Save the result document
        document.saveToFile("HTMLstringToWord.docx", FileFormat.Docx_2013);
    }
}

The output document:

Convert TXT to WORD online, for free.

Max file size 1GB.
Sign Up for more

How to Convert TXT to WORD?

  1. Click the “Choose Files” button to select your TXT files.
  2. Click the “Convert to WORD” button to start the conversion.
  3. When the status change to “Done” click the “Download WORD” button

Easy to Use

Simply upload your TXT files and click the convert button. You can also batch convert TXT to WORD format.

Best Quality

We use both open source and custom software to make sure our conversions are of the highest quality. In most cases, you can fine-tune conversion parameters using “Advanced Settings” (optional, look for the icon).

Free & Secure

Our TXT to WORD Converter is free and works on any web browser. We guarantee file security and privacy. Files are protected with 256-bit SSL encryption and automatically deleted after 2 hours.

Convert other files to WORD format

FreeConvert.com can convert your other files to WORD format:

Convert Your TXT files to other formats

FreeConvert.com can convert your TXT files to these other formats:

Want to convert large files without a queue or Ads?

Upgrade Now

An utility to process string operations. Just need to provide the text content.

Click
Here To Watch A Demo On It(https://youtu.be/gjscMFjxNSw).


It supports:

  • Convert a string to lower case /li>
  • Convert a string to upper case
  • Convert a string to word case
  • Convert multi-line string to a single line.
  • Break a string to multi-line string.
  • Convert a string to sentense case.
  • Convert a string to represent by a java variable
  • Copy a block of string starting from a fixed position
  • Convert multi line string to a single line separated by
    delimiter string (e.g. «‘,'»)


How It Works:

Best Free Online Text to Word Converter

  • To convert one file type to another, you can use this app’s feature for free. Without registration and a captcha. Here you can convert documents online and save them in the format you need on your computer or any other device.
  • Text to Word Converter is your multipurpose conversion tool for almost all popular file formats. You can make it online in seconds for free.
  • You can use our online tool without downloading any software and free of charge. Just use your browser.
  • Even though the tool is free, no one limits you by the number and size. It significantly distinguishes the Text to Word Converter from competitors.
  • Forget about malware, viruses, and storage space. With our application, you only upload the edited file and nothing else.
  • Quick and easy

    Text to Word Converter is a service for online file conversion from one type to another. We support many popular formats for work, all possible image formats, multimedia file formats, etc. Our Text to Word conversion tool is easy to use: select the desired file type, then define the output format of your document, upload the file and click ‘Upload’.

    Security guaranteed

    We guarantee secure and private. We do not get the right to your file and there will be no manual checking. We care about your privacy and your files. In this regard, we will also not share your data with other parties. It is essential that you have the opportunity to immediately delete the files you have uploaded from our server. If you forget to do this, they will be automatically deleted from our server after 24 hours. We completely secure your information.

    Universal conversion

    You can convert files to Text to Word from any OS or device with an internet connection. Our service works on any OS including Windows, Mac, and Linux.

    Most popular conversions

    We support the most common conversion options for work and study. Use our Free application to decrease your workload, when dealing with both document and image files.

    Fast and Easy Conversion

    Fast and Easy Conversion

    Upload your document, choose the save format type and click on “Convert” button. You will get the download link as soon as the file is converted.

    Convert from Anywhere

    Convert from Anywhere

    It works from all platforms including Windows, Mac, Android and iOS. All files are processed on our servers. No plugin or software installation required for you.

    Conversion Quality

    Conversion Quality

    . All files are processed using Aspose APIs, which are being used by many Fortune 100 companies across 114 countries.

    Need to convert text files to Word programmatically? With Aspose.Words for C++ any developer can convert text to Word format with just a few lines of C++ code.

    Modern document-processing C++ API creates Word from text with high speed. Test the quality of text to Word conversion right in a browser. Powerful C++ library allows converting text files to many popular formats.

    Save text as Word in C++

    The following example demonstrates how to convert text to Word in C++.

    Follow the easy steps to turn a text file into Word format. Read text file from the local drive, then simply save it as Word, specifying the required file format by Word extension. For both text reading and Word writing you can use fully qualified filenames. The output Word content and formatting will be identical to the original text document.

    How to convert text to Word

    1. Install ‘Aspose.Words for C++’.
    2. Add a library reference (import the library) to your C++ project.
    3. Open the source text file in C++.
    4. Call the ‘Save()’ method, passing an output filename with Word extension.
    5. Get the result of text conversion as Word.

    C++ library to convert text to Word

    There are three options to install Aspose.Words for C++ to your developer environment. Please choose one that resembles your needs and follow the step-by-step instructions:

    • Install a NuGet Package. See Documentation
    • Install the library using Package Manager Console within Visual Studio IDE
    • Install the library manually using Windows Installer

    System Requirements

    You can use this C++ library to develop software on Microsoft Windows, Linux and macOS operating systems:

    • GCC >= 6.3.0 and Clang >= 3.9.1 are required for Linux
    • Xcode >= 12.5.1, Clang and libc++ are required for macOS

    If you develop software for Linux or macOS, please check information on additional library dependencies (fontconfig and mesa-glu open-source packages) in Product Documentation.

    RRS feed

    • Remove From My Forums
    • Question

    • User40241534 posted

      Hi ,

      I have a string  «<b>Hello </b>» having Hello as Bold.

      I need to use this string and print «Hello» as bold in word doc . how should I implement. I didn’t want to convert .html to .doc as i am using paragraph for displaying Static Parameters.

      Thank you

    All replies

    • User-1509636757 posted

      In order to build the .doc file (which supports html) you are going to need to format the html to include indicators that office understands, and then you will need to write that format to the response. Here is a sample code that include required formatting
      along with custom html text that is being exported to word document:

      public partial class WebForm62 : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              string yourHtmlContent = "<b>Hello!.. </b><i>This is Kaptain!</i>";
              var strBody = new StringBuilder();
      
              //-- add required formatting to html
              AddFormatting(strBody, yourHtmlContent);
      
              //-- download file.. of you can write code to save word in any application folder
              DownloadWord(strBody.ToString());
                  
          }
      
          private void DownloadWord(string strBody)
          {
              HttpContext.Current.Response.Clear();
              HttpContext.Current.Response.Charset = "";
              HttpContext.Current.Response.ContentType = "application/msword";
              string strFileName = "DocumentName" + ".doc";
              HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
      
              HttpContext.Current.Response.Write(strBody);
              HttpContext.Current.Response.End();
              HttpContext.Current.Response.Flush();
          }
      
      
          private void AddFormatting(StringBuilder strBody, string yourHtmlContent)
          {
              strBody.Append("<html " +
                  "xmlns:o='urn:schemas-microsoft-com:office:office' " +
                  "xmlns:w='urn:schemas-microsoft-com:office:word'" +
                  "xmlns='http://www.w3.org/TR/REC-html40'>" +
                  "<head><title>Time</title>");
      
              //The setting specifies document's view after it is downloaded as Print
              //instead of the default Web Layout
              strBody.Append("<!--[if gte mso 9]>" +
                  "<xml>" +
                  "<w:WordDocument>" +
                  "<w:View>Print</w:View>" +
                  "<w:Zoom>90</w:Zoom>" +
                  "<w:DoNotOptimizeForBrowser/>" +
                  "</w:WordDocument>" +
                  "</xml>" +
                  "<![endif]-->");
      
              strBody.Append("<style>" +
                  "<!-- /* Style Definitions */" +
                  "@page Section1" +
                  "   {size:8.5in 11.0in; " +
                  "   margin:1.0in 1.25in 1.0in 1.25in ; " +
                  "   mso-header-margin:.5in; " +
                  "   mso-footer-margin:.5in; mso-paper-source:0;}" +
                  " div.Section1" +
                  "   {page:Section1;}" +
                  "-->" +
                  "</style></head>");
      
              strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
                  "<div class=Section1>");
              strBody.Append(yourHtmlContent);
              strBody.Append("</div></body></html>");
          }
      }

      Referenced link:
      c# — How do I convert HTML to a Word document on the fly in ASP.NET MVC?

    • User40241534 posted

      Thank you Kaushal , the above code helper

      but i want to know if i can apply this text in the paragraph field and use other static string in other paragraphs

    • User-1509636757 posted

      i can apply this text in the paragraph field and use other static string in other paragraphs

      Can you elaborate more on «apply this text in the paragraph field» and «other static string in other paragraphs»

    • User40241534 posted

      i have 2 strings 

      string 1= «<b> Hello </b>»;

      string 2 = «Company»;

      I am using string 2 in paragraph.range.text  for creating the paragraph, can i do the same for the code u have mentioned for converting html to word, apply it to paragraph2.range.text field.

      thank you 

      Saurav

    • User-1509636757 posted

      paragraph.range.text

      I am not sure in what context paragraph.range.text comes. It seems you might be using VBA/VSTO or Office Interop dll to generate word document. If so, then above code would not work.

      If in case, that you are using Office Interop then, here one way, that you store your html data temporarily in an html file and then export to word document. Once you do that you would be able to add/modify paragraph range

    • User40241534 posted

      Hi Kaushal,

      I have converted the html string to word with Spire plugin and also with interop plugin. Here is the code for reference..

      Step 1:- Create an Html file and save it to doc

       public void CreateHtmlDocument()
          {
              saveHtmlFile(HtmlCode);

              FileInfo srcFile = new FileInfo(HtmlFilename);
              FileInfo destFile = new FileInfo(DocFileName);

              Application word = new Application();

              Document doc = new Document();
              doc = word.Documents.Add();
              word.Visible = false;

              doc = word.Documents.Open(srcFile.FullName);
              doc.Activate();

              doc.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;

              doc.SaveAs2(destFile.FullName, WdSaveFormat.wdFormatDocument);
              doc.Close(false);
              word.Quit();

          }

        public void saveHtmlFile(string HtmlContent)
          {
              System.IO.File.WriteAllText(HtmlFileName, HtmlContent);
          }

      Step 2 :- Retrieve the file in paragraph

       paragraph.Range.InsertFile(DocFileName,ref missing, ref missing,ref missing,ref missing);

      Thanks

    • User-1599850035 posted

      I’d recommend you to use free Word component to insert HTML string to Word document using below code.

      Document doc = new Document();
      Section s = doc.AddSection();
      Paragraph para = s.AddParagraph();
      
      //Insert Hmtl styled text to paragraph
      string htmlString = "<b>Hello World</b>";
      para.AppendHTML(htmlString);
      
      doc.SaveToFile("InsertHtml.docx", FileFormat.Docx2013);
      

    1. Select your TXT files from device, Google Drive, Dropbox to convert

    Or enter URL of the TXT file you want to convert

    File Name Status Result
    Process

    Download

    Process

    Process

    Process

    Step 1

    Upload TXT

    Select files from Computer, URL, Google Drive, Dropbox or by dragging it on the page.

    Step 2

    Choose to Word

    Choose Word or any other format you need as a result (more than 200 formats supported)

    Step 3

    Download your Word

    Let the file convert and you can download your Word file right afterwards

    TXT

    Plaintext Text File

    TXT is a file format that contains the text, organized in rows. Text files opposed binaries that contain data that is not intended for interpretation as a text (encoded sound or image). The text file can contain both formatted and unformatted text. Since the very simple text files, they are frequently used for storing data.

    Word

    Microsoft Office Open XML

    Since 2007, Microsoft began to use a file format docx, which is created by using the Office Open XML. The format is a zip-file containing the text in the form of XML, graphics and other data that can be translated into a sequence of bits using patent-protected binary formats. At first it was assumed that this format will replace the doc, but both formats are still used today.

    Convert TXT to Word for Free

    Pdfmall’s TXT to Word converter is easy to use for free without any limitation and restriction. You don’t have to register or hand in any personal info. Besides, you can even use this service on any device, at any time and anywhere.

    File Security Protected

    Your privacy means all to us. It’s our priority to ensure its safety. None of your files or their contents will be used after the conversion. You can learn more about security from our Privacy Policy.

    Conversion in The Best Quality

    TXT to Word is not so difficult. But the quality is hard to guarantee. Fortunately, with our solutions, the best solutions provider on the market, our server can offer you the best quality to convert your TXT file to Word.

    Like this post? Please share to your friends:
  • Convert srt to word
  • Converter для word excel
  • Convert sql dates to excel
  • Converter word в пдф
  • Convert site to word