需求:

一、现有250个英语单词按1-300序号分好,需要从中随机选取18个组成一组,总共18组。(只取序号就行)然后将分好的组输出到word里。

二、因为使用者对电脑不熟悉,所以最好能用最简单的方法运行py文件(使用批处理,运行一下批处理文件即可)

思路:

因为只需要随机选取序号就行,可以生成一个列表,里面包含了1到300即可

words = range(1,250)

之后使用python下的win32com模块,将随机选取的序号写到word文件里即可。

win平台下安装win32com模块命令:

pip install pypiwin32

完整代码如下:

import random

import win32com

from win32com.client import Dispatch, constants

import itertools

import random

w = win32com.client.Dispatch('Word.Application')

#后台运行,不显示,不警告

w.Visible = 0

w.DisplayAlerts = 0

doc = w.Documents.Add()

myRange = doc.Range(0,0)

myRange.Style.Font.Name = "宋体"

myRange.Style.Font.Size = "16"

words = range(1,250)

myRange.InsertAfter('第一组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18)) #随机从words中选取18个单词插入到文件

myRange.InsertAfter('\n')

myRange.InsertAfter('第二组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第三组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第四组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第五组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第六组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第七组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第八组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第九组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十一组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十二组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十三组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十四组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('\n')

myRange.InsertAfter('第十五组:')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十六组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十七组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

myRange.InsertAfter('第十八组:')

myRange.InsertAfter('\n')

myRange.InsertAfter(random.sample(words, 18))

myRange.InsertAfter('\n')

doc.SaveAs(r'D:\d.doc') #最终文件保存在d盘

doc.Close()

w.Quit()

使用批处理文件运行py文件

因为这样写成的脚本需要在shell中运行,对于不熟悉电脑的朋友来说有点难度,所以给出一个批处理文件,只需要双击此文件就可直接生成最终的word文件。

这里,我将python文件命令为2.py,将批处理文件和py文件同时放在桌面,你可以自行修改下面的路径。

批处理文件完整代码如下:

@echo off

C:

cd C:\Users\Administrator\Desktop

start python 2.py

exit

最终效果如图:

python echo off_生活中的python-随机分配单词输出至word相关推荐

  1. python能在生活中做什么-Python能在生活中做什么

    很多人学习Python就是为了在人工智能.大数据等领域谋求一份高薪工作,Python其实很接地气,我们如果学习了Python而不把它作为一种谋生手段也可以在生活中解决很多问题,那么Python能在生活 ...

  2. python之禅 中文_《Python之禅》中对于Python编程过程中的一些建议

    <Python之禅>中对于Python编程过程中的一些建议 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  <Python之禅>中对于Pyt ...

  3. 编写程序将字符串中最长的单词输出

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p241 习题7 8.写一个函数,输入一段英文,将此字符串中最长的单词输出 #include<st ...

  4. 用一个函数来实现将一行字符串中最长的单词输出。此行字符串从主函数传递给该函数.

    /2.用一个函数来实现将一行字符串中最长的单词输出.此行字符串从主函数传递给该函数./ #include <stdio.h> #include <string.h> int m ...

  5. (C语言)输入一行字符,将此字符串中最长的单词输出。

    输入一行字符,将此字符串中最长的单词输出. #include<stdio.h> #include<string.h> int main() {char a[100], b[10 ...

  6. python能在生活中做什么-Python可以解决哪些生活中的小问题

    Python作为人工智能类的热门编程语言,近些年越来越受到关注,尤其是随着机器人.语音识别等人工智能技术的发展,Python成为继Java.C.C++之后排名第四的编程语言.但大多数人对于Java.C ...

  7. python能在生活中做什么-一行Python能实现什么丧心病狂的功能?

    开启 HTTP 服务器 python -m SimpleHTTPServer 8000 或者是 Python 3 的 python3 -m http.server 8080 开启 FTP 服务器 py ...

  8. 如何使用python编程解决生活中的问题-日常工作问题处理中Python程序的运用

    计算机应用毕业论文第八篇:日常工作问题处理中Python程序的运用 摘要:Python是一门简单.实用而且有趣的百搭款语言,在Web应用开发.系统网络运维.科学与数字计算.网络编程等领域都有所建树.在 ...

  9. cmd查看python版本-在cmd中查看python的安装路径方法

    我相信一定有很多的人跟我一样,经常忘记Python安装的路径,每当用到的时候,最笨的办法就是在全局电脑里,直接查找Python,这样是肯定能查到的,但是如果你的电脑文件超级多,这将是一个工厂量很大的事 ...

最新文章

  1. 矢量图面层和线层相交得到相交后的线层文件(gis相交)
  2. 【Linux】一步一步学Linux——bzip2recover命令(70)
  3. Ruby学习-第二章
  4. CentOS7 安装Redis Cluster集群
  5. 第二章:JAVA编程基础
  6. 前装车载导航搭载率突破50%,谁在领跑背后的导航引擎
  7. java毕业生设计资源循环利用计算机源码+系统+mysql+调试部署+lw
  8. 油猴插件安装以及好用的脚本推荐 包含电脑版本和手机版本
  9. 植物大战僵尸简单外挂原理及实现
  10. pycharm IDEA专业版2016.3.2版本和 python3.5.0 win7 64位安装包 百度云资源共享 及安装和编辑器注册图录
  11. LaTeX局部改变字体类型
  12. 计算机图形学:机器人的画法与填充
  13. 利用VirtualBox搭建私有云
  14. html 输入框并行,Python:输入文本框并行捕获userinput到OpenCV Live网络摄像头图像...
  15. 二十五条励志金句,让你迅速提升自己的价值!
  16. 【夜读】不要让今天的懒,成为你明天的难
  17. HSI、HSV、RGB、CMY、CMYK、HSL、HSB、Ycc、XYZ、Lab、YUV等颜色模型简介
  18. 站外引流——YouTube篇
  19. 下垂控制的两电平三相桥式逆变器,可根据负载的变化改变三相输出电压的频率和幅值
  20. windbg- !analyze -v 信息详解

热门文章

  1. 如何为CentOS 7配置静态IP地址
  2. python中print(chr(65))_Python语句print(chr(97))的运行结果是( )。_学小易找答案
  3. application.properties引用其他文件_企业级SpringBoot应用多个子项目配置文件规划、多环境支持(一)...
  4. 品质和创新成为 GDC 2017 “State of Unreal” 的焦点
  5. 一份帮助你更好地理解深度学习的资源清单 1
  6. python之log日志模块
  7. larvel nginx 配置
  8. Redis持久化配置
  9. 虚拟化软件Xen的简单应用
  10. jquery js 兄弟父元素的获取