<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      在 Excel 中使用 Python 自動填充公式

      安裝Python包的國內鏡像源

      清華大學
      https://pypi.tuna.tsinghua.edu.cn/simple
       
      阿里云
      https://mirrors.aliyun.com/pypi/simple/
       
      豆瓣
      https://pypi.douban.com/simple/ 
       
      百度云
      https://mirror.baidu.com/pypi/simple/
       
      中科大
      https://pypi.mirrors.ustc.edu.cn/simple/
       
      華為云
      https://mirrors.huaweicloud.com/repository/pypi/simple/
       
      騰訊云
      https://mirrors.cloud.tencent.com/pypi/simple/

      準備工作

      首先,確保你已經安裝了 openpyxl 庫。如果還沒有安裝,可以使用以下命令進行安裝:

      pip install openpyxl

      步驟 1:導入必要的庫

      首先,我們需要導入 openpyxl 庫中的 load_workbookTranslator 類。

      from openpyxl import load_workbook
      from openpyxl.formula.translate import Translator

      步驟 2:定義填充公式的函數

      接下來,我們定義一個名為 fill_down_formulas 的函數。這個函數接受以下參數:

      • filepath:Excel 文件的路徑。
      • sheetname:工作表名稱。
      • start_row:開始填充公式的行號。
      • start_column:開始填充公式的列號。
      • num_columns:需要填充公式的列數。
      def fill_down_formulas(filepath, sheetname, start_row, start_column, num_columns):
          try:
              # 加載 Excel 文件
              wb = load_workbook(filename=filepath)
              ws = wb[sheetname]

      步驟 3:獲取起始單元格的公式

      在指定的列范圍內,我們首先獲取起始單元格的公式。

              # 循環處理每一列
              for column_index in range(start_column, start_column + num_columns):
                  # 獲取起始單元格的公式
                  formula = ws.cell(row=start_row, column=column_index).value
                  print(f"原始公式 ({start_row}, {column_index}):", formula)

      步驟 4:向下填充公式

      從起始行的下一行開始,我們將公式填充到該列的其余單元格中。這里使用 Translator 類來更新公式。

                  # 從起始行開始填充公式
                  for row in range(start_row + 1, ws.max_row + 1):
                      # 獲取起始單元格和當前單元格的坐標
                      start_coordinate = ws.cell(row=start_row, column=column_index).coordinate
                      current_coordinate = ws.cell(row=row, column=column_index).coordinate
                      print("起始坐標:", start_coordinate)
                      print("當前坐標:", current_coordinate)
      
                      # 使用 Translator 解析并更新公式
                      translated_formula = Translator(formula, origin=start_coordinate).translate_formula(current_coordinate)
                      print("翻譯后的公式:", translated_formula)
                      ws.cell(row=row, column=column_index).value = translated_formula

      步驟 5:保存修改后的 Excel 文件

      填充完公式后,保存修改后的 Excel 文件。

              # 保存修改后的 Excel 文件
              wb.save(filepath)
              print(f"成功向下填充公式到第 {start_column} 列到第 {start_column + num_columns - 1} 列,起始行 {start_row}")
          except Exception as e:
              print(f"填充公式時出錯: {e}")

      步驟 6:執行腳本

      在腳本的最后,我們指定 Excel 文件路徑、工作表名稱、起始行、起始列和列數,并調用 fill_down_formulas 函數。

      if __name__ == "__main__":
          # 指定 Excel 文件路徑、工作表名、起始行、起始列和列數
          excel_file_path = "C:\\Users\\Administrator\\Desktop\\銷售系數數據同步.xlsx"
          sheet_name = "商品費用"
          start_row = 2  # 指定起始行
          start_column = 47  # 指定起始列
          num_columns = 7  # 指定要填充公式的列數
      
          # 調用函數將公式向下填充到指定列和起始行之后
          fill_down_formulas(excel_file_path, sheet_name, start_row, start_column, num_columns)

      完整代碼

      from openpyxl import load_workbook
      from openpyxl.formula.translate import Translator
      
      def fill_down_formulas(filepath, sheetname, start_row, start_column, num_columns):
          try:
              # 加載 Excel 文件
              wb = load_workbook(filename=filepath)
              ws = wb[sheetname]
      
              # 循環處理每一列
              for column_index in range(start_column, start_column + num_columns):
                  # 獲取起始單元格的公式
                  formula = ws.cell(row=start_row, column=column_index).value
                  print(f"原始公式 ({start_row}, {column_index}):", formula)
      
                  # 從起始行開始填充公式
                  for row in range(start_row + 1, ws.max_row + 1):
                      # 獲取起始單元格和當前單元格的坐標
                      start_coordinate = ws.cell(row=start_row, column=column_index).coordinate
                      current_coordinate = ws.cell(row=row, column=column_index).coordinate
                      print("起始坐標:", start_coordinate)
                      print("當前坐標:", current_coordinate)
      
                      # 使用 Translator 解析并更新公式
                      translated_formula = Translator(formula, origin=start_coordinate).translate_formula(current_coordinate)
                      print("翻譯后的公式:", translated_formula)
                      ws.cell(row=row, column=column_index).value = translated_formula
      
              # 保存修改后的 Excel 文件
              wb.save(filepath)
              print(f"成功向下填充公式到第 {start_column} 列到第 {start_column + num_columns - 1} 列,起始行 {start_row}")
          except Exception as e:
              print(f"填充公式時出錯: {e}")
      
      if __name__ == "__main__":
          # 指定 Excel 文件路徑、工作表名、起始行、起始列和列數
          excel_file_path = "C:\\Users\\Administrator\\Desktop\\銷售系數數據同步.xlsx"
          sheet_name = "商品費用"
          start_row = 2  # 指定起始行
          start_column = 47  # 指定起始列
          num_columns = 7  # 指定要填充公式的列數
      
          # 調用函數將公式向下填充到指定列和起始行之后
          fill_down_formulas(excel_file_path, sheet_name, start_row, start_column, num_columns)

       

      posted @ 2024-06-06 15:16  懸崖上的金魚  閱讀(986)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久久久无码国产精品不卡| 亚洲国产午夜福利精品| 国产一区在线播放av| 宅男噜噜噜66在线观看| 国产日韩一区二区在线| 91福利视频一区二区| 国产一区二区三区小说| 青青青国产在线观看免费| 国产美女在线精品免费观看| 国产一区二区在线有码| 日韩精品无码区免费专区 | 一本大道久久香蕉成人网| 高清自拍亚洲精品二区| 韩国19禁无遮挡啪啪无码网站| 午夜色大片在线观看免费| 亚洲色最新高清AV网站| 人妻丝袜AV中文系列先锋影音| 国产麻豆放荡av激情演绎| 中国亚州女人69内射少妇| 精品少妇爆乳无码aⅴ区| 欧洲免费一区二区三区视频| 青河县| 免费费很色大片欧一二区| 日本一区二区三区免费播放视频站| 青青青青久久精品国产| 国产自拍在线一区二区三区| 最新国内精品自在自线视频| 国产精品亚洲二区在线播放| 制服丝袜人妻有码无码中文字幕| 国产91精选在线观看| 国产自在自线午夜精品| 99久久国产成人免费网站| 国自产拍偷拍精品啪啪模特| 久久婷婷成人综合色| 一道本AV免费不卡播放| 国产久免费热视频在线观看| 亚洲综合一区二区三区| 亚洲国产精品老熟女乱码| 深夜av免费在线观看| 亚洲欧美综合人成在线| 日韩一区二区三区在线观院|