GithubHelp home page GithubHelp logo

mherkazandjian / docxsphinx Goto Github PK

View Code? Open in Web Editor NEW
27.0 27.0 10.0 594 KB

Generate word documents from sphinx documentation

Python 97.93% Dockerfile 1.06% Makefile 1.01%
docx docx-generator microsoft python3 python36 sphinx sphinx-doc word

docxsphinx's People

Contributors

hugobuddel avatar mherkazandjian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docxsphinx's Issues

Unsupported on recent Sphinx versions

This project looks great, but it's not working on recent Sphinx versions.

Here's what I get on Sphinx >v5:

# sphinx-build -M docx -l en --
<...>
writing...
Exception occurred:
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/nodes.py", line 575, in __getitem__
    return self.children[key]
IndexError: list index out of range

I got the full tracelog, clean of secret data here:

# Sphinx version: 5.2.3
# Python version: 3.8.16 (CPython)
# Docutils version: 0.15 release
# Jinja2 version: 3.1.2
# Last messages:
# <...>
#
#   writing...
# Loaded extensions:
#   sphinx.ext.mathjax (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinxcontrib/applehelp/__init__.py
#   sphinxcontrib.devhelp (1.0.2) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinxcontrib/devhelp/__init__.py
#   sphinxcontrib.htmlhelp (2.0.0) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinxcontrib/htmlhelp/__init__.py
#   sphinxcontrib.serializinghtml (1.1.5) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinxcontrib/serializinghtml/__init__.py
#   sphinxcontrib.qthelp (1.0.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinxcontrib/qthelp/__init__.py
#   alabaster (0.7.12) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/alabaster/__init__.py
#   sphinx.ext.intersphinx (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/intersphinx.py
#   sphinx.ext.graphviz (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/graphviz.py
#   sphinx.ext.autodoc.preserve_defaults (1.0) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/autodoc/preserve_defaults.py
#   sphinx.ext.autodoc.type_comment (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/autodoc/type_comment.py
#   sphinx.ext.autodoc (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/autodoc/__init__.py
#   sphinx.ext.napoleon (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/napoleon/__init__.py
#   sphinx.ext.autosummary (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/autosummary/__init__.py
#   sphinx_automodapi.autodoc_enhancements (unknown version) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx_automodapi/autodoc_enhancements.py
#   sphinx.ext.inheritance_diagram (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/inheritance_diagram.py
#   sphinx_automodapi.automodsumm (unknown version) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx_automodapi/automodsumm.py
#   sphinx_automodapi.automodapi (unknown version) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx_automodapi/automodapi.py
#   sphinx_automodapi.smart_resolver (unknown version) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx_automodapi/smart_resolver.py
#   sphinx.ext.todo (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/todo.py
#   sphinx.ext.extlinks (5.2.3) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/ext/extlinks.py
#   docxsphinx (unknown version) from $PWD/.venv/3.8-64/lib/python3.8/site-packages/docxsphinx/__init__.py
Traceback (most recent call last):
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/cmd/build.py", line 279, in build_main
    app.build(args.force_all, filenames)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/application.py", line 350, in build
    self.builder.build_update()
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 299, in build_update
    self.build(['__all__'], to_build)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 368, in build
    self.write(docnames, list(updated_docnames), method)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docxsphinx/builder.py", line 85, in write
    self.write_doc(docname, doctree)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docxsphinx/builder.py", line 90, in write_doc
    self.writer.write(doctree, destination)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/writers/__init__.py", line 78, in write
    self.translate()
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docxsphinx/writer.py", line 98, in translate
    self.document.walkabout(visitor)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/nodes.py", line 180, in walkabout
    if child.walkabout(visitor):
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/nodes.py", line 172, in walkabout
    visitor.dispatch_visit(self)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/nodes.py", line 1900, in dispatch_visit
    return method(node)
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docxsphinx/writer.py", line 1106, in visit_comment
    comment = node[0]
  File "$PWD/.venv/3.8-64/lib/python3.8/site-packages/docutils/nodes.py", line 575, in __getitem__
    return self.children[key]
IndexError: list index out of range

Hopefully this helps.

Wrong content type of a dotx template

Sphinx output:

Exception occurred:
  File "/usr/local/lib/python3.8/dist-packages/docx/api.py", line 28, in Document
    raise ValueError(tmpl % (docx, document_part.content_type))
ValueError: file 'source/internal-long-style.dotx' is not a Word file,
content type is 'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml'

What is the correct content type? My dotx file is a direct export from MS Word. Are there any limitations to versions of MS Word? Or to cloud/desktop variants of MS Office?

Error log:

# Sphinx version: 3.5.1
# Python version: 3.8.5 (CPython)
# Docutils version: 0.15 release
# Jinja2 version: 2.11.3
# Last messages:
#   reading sources... [ 66%] 01 Decision Record
#   reading sources... [100%] index
#   
#   looking for now-outdated files...
#   none found
#   pickling environment...
#   done
#   checking consistency...
#   done
#   preparing documents...
# Loaded extensions:
#   sphinx.ext.mathjax (3.5.1) from /usr/local/lib/python3.8/dist-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from /usr/local/lib/python3.8/dist-packages/sphinxcontrib/applehelp/>
#   sphinxcontrib.devhelp (1.0.2) from /usr/local/lib/python3.8/dist-packages/sphinxcontrib/devhelp/__in>
#   sphinxcontrib.htmlhelp (1.0.3) from /usr/local/lib/python3.8/dist-packages/sphinxcontrib/htmlhelp/__>
#   sphinxcontrib.serializinghtml (1.1.4) from /usr/local/lib/python3.8/dist-packages/sphinxcontrib/seri>
#   sphinxcontrib.qthelp (1.0.3) from /usr/local/lib/python3.8/dist-packages/sphinxcontrib/qthelp/__init>
#   alabaster (0.7.12) from /usr/local/lib/python3.8/dist-packages/alabaster/__init__.py
#   sphinx.ext.todo (3.5.1) from /usr/local/lib/python3.8/dist-packages/sphinx/ext/todo.py
#   docxsphinx (unknown version) from /usr/local/lib/python3.8/dist-packages/docxsphinx/__init__.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/sphinx/cmd/build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "/usr/local/lib/python3.8/dist-packages/sphinx/application.py", line 352, in build
    self.builder.build_update()
  File "/usr/local/lib/python3.8/dist-packages/sphinx/builders/__init__.py", line 293, in build_update
    self.build(['__all__'], to_build)
  File "/usr/local/lib/python3.8/dist-packages/sphinx/builders/__init__.py", line 360, in build
    self.write(docnames, list(updated_docnames), method)
  File "/usr/local/lib/python3.8/dist-packages/docxsphinx/builder.py", line 77, in write
    self.prepare_writing(docnames)
  File "/usr/local/lib/python3.8/dist-packages/docxsphinx/builder.py", line 62, in prepare_writing
    self.writer = DocxWriter(self)
  File "/usr/local/lib/python3.8/dist-packages/docxsphinx/writer.py", line 83, in __init__
    dc = Document(os.path.join('source', self.template_dir))
  File "/usr/local/lib/python3.8/dist-packages/docx/api.py", line 28, in Document
    raise ValueError(tmpl % (docx, document_part.content_type))
ValueError: file 'source/internal-long-style.dotx' is not a Word file,
content type is 'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml'

add support for sphinx >=1.8.5

There seems to be an issue when sphinx==1.8.5 is used.

try

pip install -r requirements.txt

where requirements.txt has the following content:

pytest==6.1.2
docutils==0.15
sphinx==1.8.5
python-docx>=0.8.6
nose==1.3.7

the same behavior is observed for higher versions of sphinx, e.g 3.3.0.

I think this is related to unicode related text in the .rst files. #24 seems to be related.

another issue is with logger. There is a depracation warning that has been added in sphinx==1.8.5 for that.

Image caption issue for .doxc document

With reference to a .docx document generated with the execution of the command make html docx the image caption is not displayed under the image but it is displayed after the last text available before the image call.
For example, in the code reported below, the caption Image caption is reported after General Test. resulting in General Test.Image caption.

The same issue has been detected, during the generation of the .docx document, with the image reported in the example: https://github.com/mherkazandjian/docxsphinx/blob/master/examples/sample_1/source/index.rst

Also the numbered reference :numref:`` is not working despite the usage of numfig=True in the conf.py file

Could you please help me to solve this issue?

Kind regards

Code:

`General Test.

.. _First-figure:

.. figure:: _static/Life_cycle_of_the_product.png

Image caption

An example is shown in :ref:First-figure.

  • plain reference :ref:First-figure

  • named reference :ref:XXXXX <First-figure>

  • numbered reference :numref:First-figure
    `

make proper use of the template path

In conf.py one can specify a path for the templates. E.g.

# 'docx_template' need *.docx or *.dotx template file name. default is None.
docx_template = 'template.docx'

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

However, the templates_path is ignored.

IndexError: list index out of range

Sphinx version: 3.3.1

Python version: 3.7.3 (CPython)

Docutils version: 0.16 release

Jinja2 version: 2.11.3

Last messages:

_glossary/_EN/glossary_W

_glossary/_EN/glossary_X

_glossary/_EN/glossary_Y

_glossary/_EN/glossary_Z

appendix

_appendix/appendix_downloads

_appendix/appendix_tables

_appendix/appendix_index

writing...

Loaded extensions:

sphinx.ext.mathjax (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/mathjax.py

sphinxcontrib.applehelp (1.0.2) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/applehelp/init.py

sphinxcontrib.devhelp (1.0.2) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/devhelp/init.py

sphinxcontrib.htmlhelp (1.0.3) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/htmlhelp/init.py

sphinxcontrib.serializinghtml (1.1.4) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/serializinghtml/init.py

sphinxcontrib.qthelp (1.0.3) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/qthelp/init.py

alabaster (0.7.8) from /usr/lib/python3/dist-packages/alabaster/init.py

sphinx.ext.autodoc.type_comment (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/autodoc/type_comment.py

sphinx.ext.autodoc (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/autodoc/init.py

sphinx.ext.autosummary (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/autosummary/init.py

sphinx.ext.coverage (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/coverage.py

sphinx.ext.doctest (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/doctest.py

sphinx.ext.extlinks (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/extlinks.py

sphinx.ext.githubpages (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/githubpages.py

sphinx.ext.graphviz (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/graphviz.py

sphinx.ext.ifconfig (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/ifconfig.py

sphinx.ext.imgmath (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/imgmath.py

sphinx.ext.inheritance_diagram (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/inheritance_diagram.py

sphinx.ext.intersphinx (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/intersphinx.py

sphinx.ext.napoleon (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/napoleon/init.py

sphinx_sitemap (unknown version) from /usr/local/lib/python3.7/dist-packages/sphinx_sitemap/init.py

sphinx.ext.todo (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/todo.py

sphinx.ext.viewcode (3.3.1) from /usr/local/lib/python3.7/dist-packages/sphinx/ext/viewcode.py

sphinxcontrib.swaggerdoc (unknown version) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/swaggerdoc/init.py

sphinxcontrib.programoutput (unknown version) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/programoutput/init.py

sphinxcontrib.plantuml (unknown version) from /usr/lib/python3/dist-packages/sphinxcontrib/plantuml.py

sphinxcontrib.httpdomain (unknown version) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/httpdomain.py

sphinxcontrib.openapi (None) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/openapi/init.py

sphinxcontrib.details.directive (0.1.0) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/details/directive/init.py

sphinxcontrib.needs (0.6.0) from /usr/local/lib/python3.7/dist-packages/sphinxcontrib/needs/init.py

docxsphinx (unknown version) from /usr/local/lib/python3.7/dist-packages/docxsphinx/init.py

Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/sphinx/cmd/build.py", line 280, in build_main
app.build(args.force_all, filenames)
File "/usr/local/lib/python3.7/dist-packages/sphinx/application.py", line 352, in build
self.builder.build_update()
File "/usr/local/lib/python3.7/dist-packages/sphinx/builders/init.py", line 294, in build_update
self.build(['all'], to_build)
File "/usr/local/lib/python3.7/dist-packages/sphinx/builders/init.py", line 361, in build
self.write(docnames, list(updated_docnames), method)
File "/usr/local/lib/python3.7/dist-packages/docxsphinx/builder.py", line 85, in write
self.write_doc(docname, doctree)
File "/usr/local/lib/python3.7/dist-packages/docxsphinx/builder.py", line 90, in write_doc
self.writer.write(doctree, destination)
File "/usr/local/lib/python3.7/dist-packages/docutils/writers/init.py", line 78, in write
self.translate()
File "/usr/local/lib/python3.7/dist-packages/docxsphinx/writer.py", line 98, in translate
self.document.walkabout(visitor)
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 214, in walkabout
if child.walkabout(visitor):
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 214, in walkabout
if child.walkabout(visitor):
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 214, in walkabout
if child.walkabout(visitor):
[Previous line repeated 2 more times]
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 206, in walkabout
visitor.dispatch_visit(self)
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 1995, in dispatch_visit
return method(node)
File "/usr/local/lib/python3.7/dist-packages/docxsphinx/writer.py", line 1106, in visit_comment
comment = node[0]
File "/usr/local/lib/python3.7/dist-packages/docutils/nodes.py", line 627, in getitem
return self.children[key]
IndexError: list index out of range

based on debian 10.8

Use empty template

Our current default template is not empty but has Japanese text in it. So all demo's look ugly.

Enhance config options - separate documents

Hello!

It would be very useful to have options in the conf.py to define a set of separate documents that would be generated from different "master" docs, similar to the built-in latex builder.

For example: Let's have this Sphinx project:

  • conf.py, Makefile
  • index.rst
  • documentA.rst
  • documentB.rst

I want document A and document B in separate output files and use different stylesheets.

I could define that in the conf.py as follows:

docx_documents = [
  ('documentA', 'Document01.docx', dotx_stylesheetA, toctree_only),
  ('documentB',  'Document02.docx', dotx_stylesheetB, toctree_only),
  ...
]

Then make docx would build Document01.docx from the file documentA.rst using docx_stylesheetA and Document02.docx from the file documentB.rst using docx_stylesheetB.

Perhaps code from the latex builder could be reused.

Thanks a lot for looking into this!

add support for docutils==0.16

+-------------------+----------------------------+------------+------------+
| **Prepared by:**  |                            | Date:      | Signature: |
+-------------------+----------------------------+------------+------------+
| J\. Smith         | foo         | 01/11/2020   |                         |
+-------------------+----------------------------+------------+------------+
| **Contributors:** |                            | Date:      | Signature: |
+-------------------+----------------------------+------------+------------+
| C\. Prime         | XXX                        |            |            |
|                   |                            |            |            |
| J\. Foo           | YYY                        |            |            |
+-------------------+----------------------------+------------+------------+
| **Authorised:**   |                            | Date:      | Signature: |
+-------------------+----------------------------+------------+------------+

in the above example the \. part is problematic with docutils 0.16
when parsing the .rst to a dom and then to a etree via lxml

Failure to output embedded image to docx

@hugobuddel . Thanks for your efforts with this tool.
I was trying to get two things working. Based on the *.rst file (middle below), I am able to output html and pdf with sections numbered as well as embedded images, however neither work when outputting to a docx. Is this possible, do you know? Perhaps I'm just missing some added configuration?

Thanks.

image

Wrong heading sizes for sections included via toctrees

The heading size for sections included via toctrees is rendered incorrectly in Word. Consider this example:

  • index.rst:
    *************
    Chapter title
    *************
    
    .. toctree::
    
       section
  • section.rst:
    Section title
    =============
    
    .. toctree::
    
       subsection
  • subsection.rst:
    Subsection title
    ----------------
    
    Some text goes here.

In the resulting Word file, all headings are of the same size (namely Heading 1). This is incorrect -- they should have descending header sizes.

When compiling the above example to singlehtml, the resulting HTML file does have the correct descending header sizes (namely <h1>, <h2>, and <h3>).

In contrast, when putting all the content in a single .rst file:

*************
Chapter title
*************

Section title
=============

Subsection title
----------------

Some text goes here.

the resulting Word file does have the correct header sizes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.