【问题标题】:Python asking for file path [duplicate]Python询问文件路径[重复]
【发布时间】:2022-01-24 06:30:08
【问题描述】:

您好,我制作了一个 python 脚本来将值从一个 excel 文件复制到另一个,但现在我需要提示用户,以便他们可以从文件资源管理器中选择文件,而无需更改脚本本身中的路径什么是最好的方法去做这个 ?将要使用它的人也不太了解脚本,让他们运行它的最佳虚拟证明方法是什么?

我的代码如下:

import openpyxl as xl
from openpyxl.styles import Alignment, Font

# opening the source excel file
filename = "C:\\excelProjects\\Test Meg Vanhoef.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]`
  
# opening the destination excel file 
filename1 = "C:\\excelProjects\\2223 Aanstipschema EGS v061221 - met koppeling forms.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.active
  
# calculate total number of rows in source excel file
mr = ws1.max_row

for i in range(2, mr+1):
    #B
    ws2['B{}'.format(i)] = ws1.cell(row=i, column=12).value
    #C
    ws2['C{}'.format(i)] = ws1.cell(row=i, column=13).value
    #D
    ws2['D{}'.format(i)] = ws1.cell(row=i, column=17).value
    #E
    ws2['E{}'.format(i)] = ws1.cell(row=i, column=15).value
    #F
    if ws1.cell(row=i, column=21).value == 'Andere':
        ws2['F{}'.format(i)] = ws1.cell(row=i, column=22).value
    else:
        ws2['F{}'.format(i)] = ws1.cell(row=i, column=21).value
    #G
    Y_val = ws1.cell(row=i, column=25).value
    Z_val = ws1.cell(row=i, column=26).value
    ws2['G{}'.format(i)] = f"{Y_val} {Z_val}"

    #H
    ws2.cell(row=i, column=8).value = ws1.cell(row=i, column=27).value
    #I
    AG_val = ws1.cell(row=i, column=33).value
    AB_val = ws1.cell(row=i, column=28).value
    AC_val = ws1.cell(row=i, column=29).value
    AN_val = ws1.cell(row=i, column=40).value
    AH_val = ws1.cell(row=i, column=34).value
    AJ_val = ws1.cell(row=i, column=36).value

    ws2['I{}'.format(i)] = f"{AG_val} ({AB_val}, {AC_val}) \n{AN_val} ({AH_val}, {AJ_val})"
    ws2['I{}'.format(i)].alignment = Alignment(wrapText=True)
    #J
    AF_val = ws1.cell(row=i, column=32).value
    ws1.cell(row=i, column=39).font = Font(bold=True)
    AM_val = ws1.cell(row=i, column=39).value

    ws2['J{}'.format(i)] = f'{AF_val} ({AB_val}) \n{AM_val} ({AH_val})'
    ws2['J{}'.format(i)].alignment = Alignment(wrapText=True)
    #K
    ws2['K{}'.format(i)] = ws1.cell(row=i, column=50).value
    #L
    ws2['L{}'.format(i)] = ws1.cell(row=i, column=51).value
    #M
    ws2['M{}'.format(i)] = ws1.cell(row=i, column=16).value
    #N
    ws2['N{}'.format(i)] = ws1.cell(row=i, column=41).value
    #O
    ws2['O{}'.format(i)] = ws1.cell(row=i, column=47).value
    #P
    ws2['P{}'.format(i)] = ws1.cell(row=i, column=49).value
    #Q
    ws2['Q{}'.format(i)] = ws1.cell(row=i, column=52).value
    #S
    BA_val = ws1.cell(row=i, column=53).value
    BB_val = ws1.cell(row=i, column=54).value
    CS_val = ws1.cell(row=i, column=96).value
    CT_val = ws1.cell(row=i, column=97).value
    if CS_val != 'Nederlands' and CT_val != 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met moeder: {CS_val} \nTaal met vader: {CT_val}'
    elif CT_val == 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met vader: {CT_val}'
    elif CS_val == 'Nederlands':
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val} \n\nTaal met moeder: {CS_val}'
    else:
        ws2['S{}'.format(i)] = f'{BA_val} \n{BB_val}'
    ws2['S{}'.format(i)].alignment = Alignment(wrapText=True)
    #W
    BG_val = ws1.cell(row=i, column=59).value
    BH_val = ws1.cell(row=i, column=60).value
    BI_val = ws1.cell(row=i, column=61).value
    BJ_val = ws1.cell(row=i, column=62).value
    BK_val = ws1.cell(row=i, column=63).value
    BL_val = ws1.cell(row=i, column=64).value
    BM_val = ws1.cell(row=i, column=65).value

    if BK_val == None and BM_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \n{BL_val}'
    elif BM_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \nmedicatie: {BK_val} \n{BL_val}'
    elif BK_val == None:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \n{BL_val} \nverwachtingen school: {BM_val}'
    else:
        ws2['W{}'.format(i)] = f'{BG_val} \n{BH_val} \n{BI_val} \n{BJ_val} \nmedicatie: {BK_val} \n{BL_val} \nverwachtingen school: {BM_val}'
    ws2['W{}'.format(i)].alignment = Alignment(wrapText=True)

    #AS
    BO_val = ws1.cell(row=i, column=67).value
    BP_val = ws1.cell(row=i, column=68).value
    BR_val = ws1.cell(row=i, column=70).value

    ws2['AS{}'.format(i)] = f'Hobby / talenten: {BO_val} / {BR_val} \nInteresses: {BP_val}'
    ws2['AS{}'.format(i)].alignment = Alignment(wrapText=True)

    #AU
    BS_val = ws1.cell(row=i, column=71).value
    BT_val = ws1.cell(row=i, column=72).value
    BV_val = ws1.cell(row=i, column=74).value
    BX_val = ws1.cell(row=i, column=76).value
    BZ_val = ws1.cell(row=i, column=78).value
    CB_val = ws1.cell(row=i, column=80).value
    CD_val = ws1.cell(row=i, column=82).value
    CE_val = ws1.cell(row=i, column=83).value

    ws2['AU{}'.format(i)] = f'Talenten: {BS_val} / {BV_val} / {BT_val} \nLeermoeilijkheden: {BX_val} / {BZ_val} \nVroegere begeleiding: {CB_val} / {CD_val} \nBelemmerende factoren: {CE_val}'
    ws2['AU{}'.format(i)].alignment = Alignment(wrapText=True)

    #AW
    CF_val = ws1.cell(row=i, column=84).value
    AO_val = ws1.cell(row=i, column=41).value
    CJ_val = ws1.cell(row=i, column=88).value
    CL_val = ws1.cell(row=i, column=90).value
    CN_val = ws1.cell(row=i, column=92).value
    CO_val = ws1.cell(row=i, column=93).value
    CP_val = ws1.cell(row=i, column=94).value
    CQ_val = ws1.cell(row=i, column=95).value

    if CO_val == 'ja':
        ws2['AW{}'.format(i)] = f"Karakter: {CF_val} \nMedisch: {BG_val}, {BH_val}, {BL_val}, {BM_val} \nFamiliaal: {AO_val} \nMoeilijkheden: {CI_val} / {CK_val} \nVroegere ondersteuning: {CM_val} / {CO_val} \nBelemmerende factoren: Indien \npestverleden \n{CQ_val} {CR_val}"
    else:
        ws2['AW{}'.format(i)] = f"Karakter: {CF_val} \nMedisch: {BG_val}, {BH_val}, {BL_val}, {BM_val} \nFamiliaal: {AO_val} \nMoeilijkheden: {CI_val} / {CK_val} \nVroegere ondersteuning: {CM_val} / {CO_val} \nBelemmerende factoren: Indien \n{CQ_val} {CR_val}"
    ws2['AW{}'.format(i)].alignment = Alignment(wrapText=True)

# saving the destination excel file
wb2.save(str(filename1))

【问题讨论】:

  • 你看过tkinter.filedialog吗?至于运行 Python 脚本的“虚拟证明”方式,假设每个人的系统上都安装了最新版本的 Python,并由您的 IT 部门保持最新,那么运行脚本可能只是双击它的问题如果它与python.exe 可执行文件相关联,但这是一个非常危险的设置。最好使用在正确环境中运行的批处理文件来安装脚本。
  • 欢迎来到 Stack Overflow。请阅读How to Askstackoverflow.com/help/dont-ask,并确保您了解这不是论坛。我们不在这里进行程序设计,也不处理“最佳方式”或 UI/UX 选择。

标签: python


【解决方案1】:
import os 
os.getcwd() 

返回当前工作路径。您可以将其用作输出路径的变量。

您应该要求脚本的用户将源 Excel 文件与您的 .py 文件放在同一路径中;

如果我理解正确,这应该会有所帮助。

【讨论】:

    【解决方案2】:

    如果您认为用户不是技术人员,您可以使用tkinter 窗口来选择文件。例如,下面的代码获取了 2 个文件路径,然后您就可以使用它了。

    import tkinter as tk
    from tkinter import filedialog
    
    root = tk.Tk()
    root.withdraw()
    
    source_file_path = filedialog.askopenfilename(title="Choose source file")
    target_file_path = filedialog.askopenfilename(title="Choose target file")
    

    有过滤文件类型、起始目录等选项,您可以参考文档。

    【讨论】:

      【解决方案3】:

      我会使用tkinter.filedialog 来完成这项任务,请考虑以下简单示例

      import tkinter as tk
      from tkinter import filedialog
      filepath1 = ""
      def callback():
          global filepath1
          filepath1 = filedialog.askopenfilename()
          root.destroy()
      root = tk.Tk()
      btn = tk.Button(root, text="Click to select file", command=callback)
      btn.pack()
      tk.mainloop()
      print("User selected", filepath1)
      

      如果允许您随意使用外部模块,那么您可以使用easygui,如下所示

      import easygui
      filepath = easygui.fileopenbox("Select file")
      print("User selected", filepath)
      

      为了安装easygui,只需执行pip install easygui

      【讨论】:

        猜你喜欢
        • 2017-06-16
        • 2017-12-09
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多