将以下代码:import os import pandas as pd folder_path = "D:/活动生成" file_name = "活动.xlsx" new_file_name = "一口价3.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) # convert product code to 7 digits product_code = str(row["商品编码"]).zfill(7) new_price_df.loc[index] = [product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 生成一个exe文件,并可以让我下载使用
时间: 2024-03-03 07:48:34 浏览: 23
为了将Python代码转换为可执行文件(exe文件),你可以使用PyInstaller库。首先,你需要在命令行中安装PyInstaller库:
```
pip install pyinstaller
```
安装完成后,打开命令行,进入代码所在目录,运行以下命令:
```
pyinstaller --onefile filename.py
```
其中,`filename.py`是你要打包成可执行文件的Python文件名。执行完这个命令后,PyInstaller将会在同一目录下生成一个`dist`文件夹,里面就是可执行文件。你可以将`dist`文件夹中的`filename.exe`文件发送给用户使用。
需要注意的是,如果你的Python文件中用到了第三方库,那么在生成可执行文件之前,你需要先将这些库打包进来。你可以使用以下命令生成一个包含所有依赖库的可执行文件:
```
pyinstaller --onefile --add-data "path/to/library;." filename.py
```
其中,`path/to/library`是你要打包进来的依赖库的路径。如果有多个依赖库,可以多次使用`--add-data`参数。
相关问题
代码:import os import pandas as pd folder_path = "D:/活动生成" file_name = "活动.xlsx" new_file_name = "一口价3.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) # only save the 7-digit 商品编码 product_code = str(row["商品编码"]) if len(product_code) == 7: new_price_df.loc[index] = [product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 没法保存商品编码为0开头的商品编码,例如商品编码为:0311069的商品编码不能保存,我需要将0开头的商品编码能进行计算的同时也保存下来
可以将商品编码转换为字符串后,判断字符串长度是否为7位,如果不足7位,在前面添加相应数量的0,使其长度为7位。修改后的代码如下:
```
import os
import pandas as pd
folder_path = "D:/活动生成"
file_name = "活动.xlsx"
new_file_name = "一口价3.xlsx"
activity_file_path = os.path.join(folder_path, file_name)
activity_df = pd.read_excel(activity_file_path)
discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39}
new_price_df = pd.DataFrame(columns=["商品编码", "价格"])
for index, row in activity_df.iterrows():
if row["活动"] in discounts:
standard_price = row["标准售价"]
discount = discounts.get(row["活动"], 1)
new_price = round(standard_price * discount, 2)
# convert product code to 7 digits
product_code = str(row["商品编码"]).zfill(7)
new_price_df.loc[index] = [product_code, new_price]
new_file_path = os.path.join(folder_path, new_file_name)
new_price_df.to_excel(new_file_path, index=False)
print("已生成")
```
这样就可以保存商品编码为0开头的商品编码了。
import os import pandas as pd from openpyxl import Workbook folder_path = r'D:/拆分后的工作簿' new_path = r'D:\拆分后的工作簿\新增資料夾\workbook.xlsx' files = os.listdir(folder_path) wb = Workbook() for file in files: if file.endswith('.xlsx'): file_path = os.path.join(folder_path, file) sheet_name = os.path.splitext(file)[0] df = pd.read_excel(file_path, sheet_name=None) for sheet in df: df[sheet].to_excel(wb, sheet_name=sheet_name)
这段代码看起来没有问题,不过你最后没有保存工作簿。你需要将以下代码添加到末尾:
```python
wb.save(new_path)
```
这将工作簿保存到指定的路径下。完整代码如下:
```python
import os
import pandas as pd
from openpyxl import Workbook
folder_path = r'D:/拆分后的工作簿'
new_path = r'D:\拆分后的工作簿\新增資料夾\workbook.xlsx'
files = os.listdir(folder_path)
wb = Workbook()
for file in files:
if file.endswith('.xlsx'):
file_path = os.path.join(folder_path, file)
sheet_name = os.path.splitext(file)[0]
df = pd.read_excel(file_path, sheet_name=None)
for sheet in df:
df[sheet].to_excel(wb, sheet_name=sheet_name)
wb.save(new_path)
```
希望这次能够帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)