Parsing and generating Microsoft Office 2007 files

2019-01-22 00:13发布

I have a web project where I must import text and images from a user-supplied document, and one of the possible formats is Microsoft Office 2007. There's also a need to generate documents in this format.

The server runs CentOS 5.2 and has PHP/Perl/Python installed. I can execute local binaries and shell scripts if I must. We use Apache 2.2 but will be switching over to Nginx once it goes live.

What are my options? Anyone had experience with this?

4条回答
疯言疯语
2楼-- · 2019-01-22 00:33

The python docx module can generate formatted Microsoft office docx files from pure Python. Out of the box, it does headers, paragraphs, tables, and bullets, but the makeelement() module can be extended to do arbitrary elements like images.

from docx import *
document = newdocument()

# This location is where most document content lives 
docbody = document.xpath('/w:document/w:body',namespaces=wordnamespaces)[0]

# Append two headings
docbody.append(heading('Heading',1)  )   
docbody.append(heading('Subheading',2))
docbody.append(paragraph('Some text')
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-22 00:37

You can probably check the code for Sphider. They docs and pdfs, so I'm sure they can read them. Might also lead you in the right direction for other Office formats.

查看更多
Emotional °昔
4楼-- · 2019-01-22 00:44

I have successfully used the OpenXML Format SDK in a project to modify an Excel spreadsheet via code. This would require .NET and I'm not sure about how well it would work under Mono.

查看更多
Animai°情兽
5楼-- · 2019-01-22 00:46

The Office 2007 file formats are open and well documented. Roughly speaking, all of the new file formats ending in "x" are zip compressed XML documents. For example:

To open a Word 2007 XML file Create a temporary folder in which to store the file and its parts.

Save a Word 2007 document, containing text, pictures, and other elements, as a .docx file.

Add a .zip extension to the end of the file name.

Double-click the file. It will open in the ZIP application. You can see the parts that comprise the file.

Extract the parts to the folder that you created previously.

The other file formats are roughly similar. I don't know of any open source libraries for interacting with them as yet - but depending on your exact requirements, it doesn't look too difficult to read and write simple documents. Certainly it should be a lot easier than with the older formats.

If you need to read the older formats, OpenOffice has an API and can read and write Office 2003 and older documents with more or less success.

查看更多
登录 后发表回答