Skip to content

xwy-bit/Xdocx

Repository files navigation

Xdocx

Python library forked from bayoo-docx. (original repository python-docx)

DEMO

Description

add some features to the original library.(To Be Continued ...)

High Level API

  • add comment in any position of the paragraph
  • add insert in any position of the paragraph

Low Level API

  • Insert class & openxml element
  • Delete class & openxml element

Installation

git clone https://github.com/xwy-bit/Xdocx.git
cd Xdocx
pip install .

Usage

Insert Demo

[Method]

paragraph.add_insert_by_range

INPUT:
    @ text : the text you want to insert
    @ ins_index : the index of the insert position. The index is the text position in the paragraph. The index is start from 0.

OUTPUT:
    @ None

[Demo]

from docx.text.paragraph import Paragraph
import docx

doc = docx.Document('path/to/original.docx')
for idx , para in enumerate(doc.paragraphs):
    # you can use other method to select the paragraph , in this demo , we just use the index
    if idx == 6:
        print('=' * 40,'paragraph',idx,'=' * 40)
        '''
        example:
        ABCD  EFG 
            ⬆️ (TEST TEXT)
        '''
        para.add_insert_by_range(text = 'TEST TEXT' , ins_index = 4)
        print(para._element.xml)

doc.save('path/to/Insert_Demo.docx')

Comment Demo

[Method] paragraph.add_cross_paragraph_comment_start_by_textidx()

INPUT: 
    @ author
    @ Initial
    @ DATE / TIME
    @ Comment TEXT
    @ Charactor Index
comment , comment_id = doc.paragraphs[6].\
    add_cross_paragraph_comment_start_by_textidx(
        'Wayen Xu',
        'WX', '2024-01-01T00:00:00Z', 'This is a comment,hahaha', 15)
doc.paragraphs[8].add_cross_paragraph_comment_end_by_textidx(comment_id,3)

About

A repository to edit docx file by python

Topics

Resources

License

Stars

Watchers

Forks