openpyxl read images and hyperlinks - detailed version

Online data is relatively small, their approach may be stupid, but adhere to the original.

Scenario is to generate word document on a predetermined format by reading the contents of the grid to excel, the document format for some industries demanding ~~ have a certain significance.

add_paragraph and add_run such basis did not say, CSDN has been a lot, mainly to address how openpyxl across the sheet reading tables, pictures and other data .

Using Hyperlinks

Xlsx own create a table in the second sheet, right after the selected tabular data - define the location, you can see a reference position

=Sheet2!$A$1:$B$5

My program is to read sheet1 content, so the sheet insert a hyperlink, select the local file location -sheet2, you just copied into the reference position - OK.

Such hyperlinks in sheet1, click after the jump to sheet2, and hyperlinks read by openpyxl, to get this reference position. You can also try without reference to the result of the location. (In fact, the role of anchor and similar)

After this you have a reference position, regular expressions can be used to obtain a python jump plaid content which open sheet2 to read, this is the content of the general reading table, and not repeat them.

eval={'A':1,'B':2,'C':3,'D':4,'E':5,'F':6}  #翻译格子数的字典
wb=load_workbook('1.xlsx')
sheet=wb['Sheet1']
t=sheet.cell(7,5).hyperlink.location        #通过超链接得到引用位置
res1=re.findall("!(.*?):",t)[0]            #通过正则得到格子左上角
res2=re.findall(':(.*?)$',t)[0]            #通过正则得到格子右下角
r1=str(eval[res1[1]])+' '+res1[3:]        #翻译成第几行、第几列(可选)
r2=str(eval[res2[1]])+' '+res2[3:]        #同上

I was split ============== =============

Pictures hyperlink relatively simple

Insert hyperlinks directly in sheet1, the python is read out of the picture with the name of the hyperlink.

The pictures on the same program with py a folder can be inserted directly into the docx file.

 

There is a better way to welcome the exchange.

Published 20 original articles · won praise 0 · Views 2038

Guess you like

Origin blog.csdn.net/weixin_37281967/article/details/104116755