To convert a OneNote (.one) file to a text (.txt) file using GroupDocs in Python, you can utilize the WordProcessingConvertOptions class to specify the output format. Here’s a sample implementation that demonstrates how to achieve this:
from groupdocs_conversion import Converter
from groupdocs_conversion.options import WordProcessingConvertOptions
def convert_one_to_txt(input_file, output_file):
# Load the source ONE file
with Converter(input_file) as converter:
# Set the conversion options to TXT format
options = WordProcessingConvertOptions(format='Txt')
# Perform the conversion
converter.convert(output_file, options)
# Example usage
input_file = "path/to/your/file.one"
output_file = "path/to/your/output/file.txt"
convert_one_to_txt(input_file, output_file)
print("Conversion to txt completed successfully.")
In this code:
You first import the necessary classes from the GroupDocs library.
The convert_one_to_txt function takes the input OneNote file and the desired output text file path.
The WordProcessingConvertOptions is configured to specify that the output format should be TXT.
Finally, the conversion is executed, and a success message is printed.
Make sure to replace "path/to/your/file.one" and "path/to/your/output/file.txt" with the actual paths to your files.
If you are still facing issues achieving the same results as the web application, ensure that you are using the correct version of the GroupDocs library and that your input file is properly formatted. You may also want to check the documentation for any additional options that could affect the conversion output.
For more details on converting OneNote files to text format, you can refer to the official documentation here.
options = WordProcessingConvertOptions(format='Txt')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: function takes at most 0 keyword arguments (1 given)
Thank you for sharing the solution. Let me add the complete code snippet for other users:
from groupdocs.conversion import Converter
from groupdocs.conversion.options.convert import WordProcessingConvertOptions
from groupdocs.conversion.filetypes import WordProcessingFileType
def convert_one_to_txt():
# Instantiate Converter with the input document
with Converter("./sample.one") as converter:
# Instantiate convert options to define the output format
word_convert_options = WordProcessingConvertOptions()
word_convert_options.format = WordProcessingFileType.TXT
# Convert ONE to TXT
converter.convert("./output.txt", word_convert_options)
if __name__ == "__main__":
convert_one_to_txt()
I have also added a section to the documentation that shows how to set output format within format family - Example 2: Specify Output Format