from openpyxl import load_workbook from docx import Document # 加載Excel文件和Word文件 excel_file = r'C:\Users\Thinkpad\Desktop\資料整編2023110--V6.xlsx' word_file = r'C:\Users\Thinkpad\Desktop\1.docx' # 打開Excel文件并選擇要插入的區(qū)域 wb = load_workbook(excel_file) ws = wb['水位特征值統(tǒng)計'] # 假設(shè)要插入的區(qū)域在Sheet1中 data = ws['B1:H26'] # 假設(shè)要插入的區(qū)域 # 打開Word文件 doc = Document(word_file) # 在指定位置插入表格 table = doc.add_table(rows=len(data), cols=len(data[0])) for i, row in enumerate(data): for j, cell in enumerate(row): table.cell(i, j).text = str(cell.value) # 保存Word文件 doc.save(r'C:\Users\Thinkpad\Desktop\1.docx')
文章來源地址http://www.zghlxwxcb.cn/news/detail-524940.html
文章來源:http://www.zghlxwxcb.cn/news/detail-524940.html
到了這里,關(guān)于python 實現(xiàn)選擇excel相關(guān)區(qū)域插入到word文件中的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!