将数据从Excel传输到Python 3中的Word

时间:2018-11-20 12:32:28

标签: python excel python-3.x csv file-handling

我正在尝试编写一个脚本,该脚本允许我从输入的excel文件中读取数据(保存为.csv格式,因为有人告诉我这样做),然后将这些数据的选定部分写入到Word文档中

这是一个脚本,用于为参与者的进餐选择创建个性化的送餐单(这些选择已累积添加到输入电子表格中)。

到目前为止,我已经创建了一个虚拟输入电子表格,并保存了一个空白的虚拟输出单词文件(分别为dummy.csv和dummy.txt)。

到目前为止,我编写的代码将电子表格读入终端,并通过一些格式对其进行了整理。

import csv
f = open("dummy.csv")
csv_f = csv.reader(f)
for row in csv_f:
    print('{:<15}  {:<15}  {:<20} {:<25}'.format(*row))

输出看起来像这样:(为方便起见,虚拟餐的选择保持不变)

Participant ID   Breakfasts       Lunches/dinners      Snacks
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111             Full english     Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
1111                              Risotto              Granola
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222             Avocado toast    Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple
2222                              Bean chilli          Apple

我的下一个挑战是以某种方式将此数据写入参与者1111,另一个参与者2222的word文件中,依此类推。我不希望脚本将这些行中的确切数据写入Word文件,而是如果输入文件中的食物选择不同,则这些行中的数据可以是什么。

最好在输出的交货单上将餐点分为早餐,午餐/晚餐和零食。

我以后可以整理字体等,我只希望现在可以选择餐食。 我也想说“ 7 x Full english”,而不是“ Full english,Full english,Full english等”。

感谢您的阅读,我们将不胜感激!

基兰

1 个答案:

答案 0 :(得分:0)

仅显示您可以使用pandas进行示范的地方:

import pandas as pd

df = pd.read_csv('whereverfilemayroam/filename')

    Participant ID     Breakfasts Lunches/dinners   Snacks
0             1111   Full english         Risotto  Granola
1             1111   Full english         Risotto  Granola
2             1111   Full english         Risotto  Granola
3             1111   Full english         Risotto  Granola
4             1111   Full english         Risotto  Granola
5             1111   Full english         Risotto  Granola
6             1111   Full english         Risotto  Granola
7             1111           None         Risotto  Granola
8             1111           None         Risotto  Granola
9             1111           None         Risotto  Granola
10            1111           None         Risotto  Granola
11            1111           None         Risotto  Granola
12            1111           None         Risotto  Granola
13            1111           None         Risotto  Granola
14            2222  Avocado toast     Bean chilli    Apple
15            2222  Avocado toast     Bean chilli    Apple
16            2222  Avocado toast     Bean chilli    Apple
17            2222  Avocado toast     Bean chilli    Apple
18            2222  Avocado toast     Bean chilli    Apple
19            2222  Avocado toast     Bean chilli    Apple
20            2222  Avocado toast     Bean chilli    Apple
21            2222           None     Bean chilli    Apple
22            2222           None     Bean chilli    Apple
23            2222           None     Bean chilli    Apple
24            2222           None     Bean chilli    Apple
25            2222           None     Bean chilli    Apple
26            2222           None     Bean chilli    Apple
27            2222           None     Bean chilli    Apple

如果您愿意,这就是您在pandas数据框中的文件,pandas中的标准容器。现在您可以用它来进行大量的统计工作了……只需在docs
上稍加挖掘即可 例子:

df.groupby(['Participant ID', 'Breakfasts']).Breakfasts.count()

Participant ID  Breakfasts   
1111            Full english     7
2222            Avocado toast    7
Name: Breakfasts, dtype: int64

df.groupby(['Participant ID', 'Lunches/dinners'])['Lunches/dinners'].count()

Participant ID  Lunches/dinners
1111            Risotto            14
2222            Bean chilli        14
Name: Lunches/dinners, dtype: int64

您当然可以按参与者ID分开

oneoneoneone = df[df['Participant ID'] == 1111]

oneoneoneone

    Participant ID    Breakfasts Lunches/dinners   Snacks
0             1111  Full english         Risotto  Granola
1             1111  Full english         Risotto  Granola
2             1111  Full english         Risotto  Granola
3             1111  Full english         Risotto  Granola
4             1111  Full english         Risotto  Granola
5             1111  Full english         Risotto  Granola
6             1111  Full english         Risotto  Granola
7             1111          None         Risotto  Granola
8             1111          None         Risotto  Granola
9             1111          None         Risotto  Granola
10            1111          None         Risotto  Granola
11            1111          None         Risotto  Granola
12            1111          None         Risotto  Granola
13            1111          None         Risotto  Granola


oneoneoneone.to_csv('target_file')

也许

twotwotwotwo.to_csv('another_target_file')

还可以遍历各个组,然后对每个组始终应用相同的操作。
例如:

for name, group in df.groupby('Participant ID'):
    print(name)
    print(group.groupby('Breakfasts').Breakfasts.count().to_string())
    print(group.groupby('Lunches/dinners')['Lunches/dinners'].count().to_string())
    print(group.groupby('Snacks').Snacks.count().to_string(), '\n')

返回:

1111
Breakfasts
Full english    7
Lunches/dinners
Risotto    14
Snacks
Granola    14 

2222
Breakfasts
Avocado toast    7
Lunches/dinners
Bean chilli    14
Snacks
Apple    14