質問

Is there a way to change the text-formatting of a .odt file using a programming software? I'm trying to make some text editions which is time consuming to be done manually in lib.office (text is big). Using R or python I can edit spaces and carriage breaks using brute force, but, to do edits like left, right-justify etc., I have to finally go back to the editor to do it. I can see there are functions in python to include tabs, change case etc., but is it possible to do right-left-centre justification in a .odt text file indirectly using a programming software?

役に立ちましたか?

解決

This is a great job for a scripting language like Python. I think you want string methods like str.ljust. That method left-justifies strings. to open the file.

Alternately, you might try defining a macro in OpenOffice (if they exist and your task is simple) - or investing some time in learning emacs or something and using that (this link shows at least some degree of emacs support for .odt files). Or learn vim, that is the one true way!

Edit: after some research I found this. It seems you could unzip the .odt file, read files within it, and manually edit the text nodes of the XML there. However, it seems like it might be easier to use a library - here are two:

https://pypi.python.org/pypi/odfpy

http://ooopy.sourceforge.net/

Edit 2: in terms of actually justifying the text, presuming you have extracted it, this:

def justify(string, left=True):
if left:
    return "\n".join(line.lstrip() for line in string.splitlines())
else:
    lines = string.splitlines()
    longest_line = len(max(lines, key=len))
    return "\n".join(line.rjust(longest_line) for line in lines)

should work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top