• lums
    2023-06-11 来自湖北
    from docx import Document from openpyxl import Workbook,load_workbook Workbook1=load_workbook("import.xlsx",data_only=True) wb_sheet_actived = Workbook1['Sheet1'] document = Document() table = document.add_table(rows=0, cols=wb_sheet_actived.max_column) table.style='Table Grid' for row_cells in wb_sheet_actived.rows: table_cells = table.add_row().cells i=0 for cell in row_cells: # print(cell.value) # print(type(cell.value)) cell_value = str(cell.value) if cell.value is not None else "" table_cells[i].text = cell_value i += 1 document.save('export.docx') 教师请教一个问题,如果读取的表格中有合并的单元格,如何把它获取出来,并写入到docx文件中呢
    展开

    作者回复: 我提供一个代码样例: from docx import Document from openpyxl import load_workbook # 加载Excel文件 Workbook1 = load_workbook("import.xlsx", data_only=True) wb_sheet_actived = Workbook1['Sheet1'] # 创建docx文档 document = Document() table = document.add_table(rows=0, cols=wb_sheet_actived.max_column) table.style = 'Table Grid' # 获取合并单元格的信息 merged_cells_ranges = wb_sheet_actived.merged_cells.ranges # 遍历Excel表格的行 for row_cells in wb_sheet_actived.rows: table_cells = table.add_row().cells i = 0 # 遍历每行的单元格 for cell in row_cells: cell_value = "" # 判断单元格是否属于合并单元格 for merged_range in merged_cells_ranges: if cell.coordinate in merged_range: # 获取合并单元格的首个单元格的值 cell_value = merged_range.start_cell.value break # 如果单元格不属于合并单元格,则获取单元格的值 if not cell_value: cell_value = str(cell.value) if cell.value is not None else "" # 将值写入到docx表格的单元格中 table_cells[i].text = cell_value i += 1 # 保存docx文件 document.save('export.docx') 上述代码使用`openpyxl`库的`merged_cells`属性获取Excel中的合并单元格范围,并检查当前单元格是否在合并范围内,如果在,则获取合并范围的首个单元格的值。 然后,将单元格的值写入到`docx`文件的表格中,处理合并单元格时替换为合并范围首个单元格的值。 ps: 可以在讨论群中和我沟通,留言区问题经常会被打卡信息给淹没,导致问题被忽略,我也是翻查视频才看到的问题

    
    
  • hwj
    2023-05-10 来自北京
    老师请叫一个问题,如何对word中图片批量添加题注

    作者回复: import win32com.client as win32 from win32com.client import constants import os curr_path = os.getcwd() doc_app = win32.gencache.EnsureDispatch('Word.Application')#打开word应用程序 doc_app.Visible =1#设置应用程序可见 doc = doc_app.Documents.Open(r'%s\批量插入题注示例文档.docx'%curr_path)#打开文档 print(doc.InlineShapes.Count) # 插入图片题注 for pic in doc.InlineShapes: rng = pic.Range rng.InsertCaption(Label= constants.wdCaptionFigure,Position=constants.wdCaptionPositionBelow,Title="咖啡杯") 参考链接: https://zhuanlan.zhihu.com/p/480345307

    
    
  • Greenery
    2023-07-27 来自新加坡
    #%% from os import chdir from openpyxl import load_workbook chdir(r'C:\Projects\python\0base_py\ch12\exp1') wb = load_workbook(filename = 'hk.xlsx') sheet_ranges = wb['Sheet1'] #%% values=[] for i in range(1,9): row = [] for j in ['A','B','C','D']: row.append(sheet_ranges[j+str(i)].value) values.append(row) print(values) #%% from docx import Document document = Document() document.add_heading('用户权限表', 0) table=document.add_table(rows=1,cols=4) hdr_cells = table.rows[0].cells for i in range(4): hdr_cells[i].text = values[0][i] for i in range(2,8): row_cells = table.add_row().cells for j in range(4): row_cells[j].text = str(values[i][j]) document.add_page_break() # 保存文件名 document.save('hk.docx')
    展开
    
    
  • Geek_fb1a3e
    2023-03-09 来自福建
    from docx import Document from openpyxl import workbook, load_workbook wb = load_workbook('12\excel1.xlsx') # print(wb) #wb.sheetnames: 取得工作簿中所有表名的列表 ws = wb[wb.sheetnames[0]] # ws.calculate_dimension(): 获取工作表全部数据的单元格区域 # ws.max_row 获取工作表最大行 # ws.max_column 获取工作表最大列 # ws.rows 迭代所有行 # ws.columns 迭代所有列 cell_area = ws[ws.calculate_dimension()] records = [] for row in cell_area: row_list = [] for cell in row: row_list.append(cell.value) records.append(row_list) document = Document() #表格:document.add_table()添加指定行数列数的表格 max_row = ws.max_row max_col = ws.max_column print(f"行数:{max_row}, 列数{max_col}") table = document.add_table(rows=0, cols=5) for i in range(max_row): row_cells = table.add_row().cells for j in range(max_col): row_cells[j].text = str(records[i][j]) document.save('toWord.docx')
    展开
    
    
  • Cy23
    2023-01-31 来自辽宁
    from docx import Document from openpyxl import Workbook,load_workbook wb = load_workbook("1.xlsx") ws = wb.active data_row = [] for row in ws.values: data_row.append(row) document = Document() table = document.add_table(rows = 0, cols = ws.max_column) table.style = 'Table Grid' for row in data_row: row_cells = table.add_row().cells for index, val in enumerate(row): row_cells[index].text = str(val) document.save('1.docx')
    
    
  • Matthew
    2023-01-28 来自江苏
    from docx import Document from openpyxl import Workbook,load_workbook # 打开要访问的 Excel wb = load_workbook("1.xlsx") ws = wb.active data_row = [] # 遍历 Excel 默认 SHEET 的每一行,存入 data_row for row in ws.values: data_row.append(row) ########################################################### # 创建要写入的 Word document = Document() table = document.add_table(rows = 0, cols = ws.max_column) table.style = 'Table Grid' # 遍历 data_row 的每个元素(元组),为 table 的每一行逐一赋值 for i, j, k in data_row: row_cells = table.add_row().cells row_cells[0].text = str(i) row_cells[1].text = str(j) row_cells[2].text = str(k) document.save('111.docx')
    展开
    
    
  • Matthew
    2023-01-28 来自江苏
    from docx import Document from openpyxl import Workbook,load_workbook # 打开要访问的 Excel wb = load_workbook("1.xlsx") ws = wb.active # 创建要写入的 Word document = Document() table = document.add_table(rows = ws.max_row, cols = ws.max_column) table.style = 'Table Grid' # 遍历 Excel 默认 SHEET 的每一个单元格,然后写入 Word for i in range(ws.max_row): row_cells = table.rows[i].cells for j in range(ws.max_column): row_cells[j].text = str(ws.cell(i+1, j+1).value) document.save('1.docx')
    
    
  • Matthew
    2023-01-28 来自江苏
    from docx import Document from openpyxl import Workbook,load_workbook # 打开要访问的 Excel wb = load_workbook("1.xlsx") ws = wb.active # 创建要写入的 Word document = Document() # 遍历 Excel 默认 SHEET 的每一个单元格,然后写入 Word for row in ws.iter_rows(): for cell in row: print(cell.value) document.add_paragraph(str(cell.value)) document.save('1.docx')
    
    
  • PatrickL
    2023-01-26 来自上海
    import openpyxl import docx wb = openpyxl.load_workbook('.\极客时间_零基础学Python_2023\\12\sample.xlsx') ws = wb[wb.sheetnames[0]] document = docx.Document() table = document.add_table(rows=ws.max_row, cols=ws.max_column) for i in range(ws.max_row): row_cells = table.rows[i].cells for j in range(ws.max_column): row_cells[j].text = str(ws.cell(i+1,j+1).value) document.save('零基础学Python_第94讲.docx')
    
    