python makes FPGA EG01 constraint file text format batch change

python makes FPGA EG01 constraint file text format batch change

All mechanical repetitive work should be automated

These two days, I studied FPGA with YJJ. I was also the first time to contact vivado and verilog. It's really big for me to write programs. There is an EG01 constraint file, which is used to configure pins. However, the format of the original file cannot be used directly. It needs to be modified. A 17 page document, one by one, can't be changed until the epidemic is over -- all the mechanical repetitive work should be automated. Adhering to this concept, I boasted: "let me go!" So we use python to make FPGA EG01 constraint file text format batch change.

Let's see what we're going to do

It turns out that this document is all like the last line. We need to convert it into the above two lines.
Do the task breakdown again
1. Read the original file
2. Mark important information
3. Extract important information
4. Reshape the format
5. Export and save

Finally, make an environmental description
The programming environment win10+vscode+python3.8.1

1. Read the original file

The EG01 constraint file of FPGA is in docx format. python needs to load pyhton docx package before reading.
win+R opens the operation box, type cmd to open the command window, enter the path to python loading package, that is, the script folder under the installation path, this is my path, cd is to enter the folder operation, and the later path needs to be replaced by itself

cd C:\Users\yuuki\AppData\Local\Programs\Python\Python38-32\Scripts

Download Python docx package with pip

pip install python-docx

After these preparations, open the editor vscode and import the package we just downloaded to operate the word document

from docx import Document
from docx.shared import Inches

Load word document

EG01 = Document('E:\yjj\EGO1 Constraint file.docx')  #Open EGO1 constraint file.docx

To verify the effect

for paragraph in EG01.paragraphs:   #Traverse each segment
    print(paragraph.text)  #Print the text of each paragraph


ok, no problem. The terminal shows that the document has been imported successfully by segment

2. Mark important information

By analyzing the characteristics of the text, we find that the key information is the prominent part in the first figure of this paper, and the context closely connected with this part has something in common. It's the easiest to use each single existing special symbol to segment the string, so we preprocess the text information first, and replace the common place of the key information context with every in word The unique special symbol of the segment (several sentences in the document are not in the same format and need to be changed manually to ensure the integrity and correctness of the extracted information). In this example, the "*" and "+" signs are used

3. Extract important information

Next, we need to extract the title and key information in batch

for paragraph in EG01.paragraphs:   #Traverse each segment
    a = paragraph.text.split('*')  #Extract key information (marked in file)
    b = paragraph.text.split('+')
    if len(a) == 1:	#Validation of extraction results
        print(a[0])
    else:
        print(a[1],b[1])

Check the extraction result at the terminal, ok

4. Reshape the format

Write the content template of each segment of word file according to the target format and embed the title and key information

for paragraph in EG01.paragraphs:   #Traverse each segment
    #print(paragraph.text)  #Print the text of each paragraph
    a = paragraph.text.split('*')  #Extract key information (marked in file)
    b = paragraph.text.split('+')
    if len(a) == 1:
        yjj.add_paragraph(a[0]) #Print split title
    else:
        yjj.add_paragraph('set_property PACKAGE_PIN' + a[1] + '[get_ports{' + b[1] + '}]')  #Print text according to new format requirements
        yjj.add_paragraph('set_property IOSTANDARD LVCMOS33' + '[get_ports{' + b[1] + '}]')

5. Export and save

yjj.save("E:\yjj\yjj.docx") #Save file

Let's take a look at the end result

ok, finish the job

The complete procedure is as follows:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'Yuuki'

from docx import Document
from docx.shared import Inches
import re

EG01 = Document('E:\yjj\EGO1 Constraint file.docx')  #Open EGO1 constraint file.docx
yjj = Document()    #create a new file
for paragraph in EG01.paragraphs:   #Traverse each segment
    #print(paragraph.text)  #Print the text of each paragraph
    a = paragraph.text.split('*')  #Extract key information (marked in file)
    b = paragraph.text.split('+')
    if len(a) == 1:
        yjj.add_paragraph(a[0]) #Print split title
    else:
        yjj.add_paragraph('set_property PACKAGE_PIN' + a[1] + '[get_ports{' + b[1] + '}]')  #Print text in new format
        yjj.add_paragraph('set_property IOSTANDARD LVCMOS33' + '[get_ports{' + b[1] + '}]')
        
yjj.save("E:\yjj\yjj.docx") #Save file
Published 1 original article, praised 0, visited 8
Private letter follow

Tags: Python pip Verilog Programming

Posted on Fri, 06 Mar 2020 23:36:33 -0800 by ina